How to pass variables and FormData with Jquery Ajax? - php

I want to update data of input field with ajax. Being new to Ajax, I've tried this so far. I am passing two variables(i.e, Name and Col to identify the name and id of the input field). So now I am stuck with this. I want to update the value of the input field with name="email_id" and Column name in mysql table "Email". So I passed the two variable so that php can identify the input name. But how do I pass the new data in the field? I have done it in another form with FormData but I need help in this case. And I apologise if I am not being specific, its my first query on stackoverflow.
HTML:
<input type="email" onblur="validate()" value="<?php echo $row['Email']; ?>" id="email_id" data-toggle="tooltip" title="Invalid Email" data-placement="top" data-content="Email must be in the format xyz#abc.com/net/info/org/in" name="email_id" placeholder="E-mail ID eg. max#mymail.com" class="form-control col-sm">
Jquery:
$('input').on("change",function(){
console.log("Clicked");
var Name=$(this).attr('name');
var Col=$(this).attr('id');
console.log(Name);
$.ajax({
url:"update.php",
method:"post",
data:{"Col":Col,"Name":Name},
success:function(response){
console.log(response);
}
});
});

You can use document.getElementByID().value to grab the value of the input field:
$('input').on("change",function(){
console.log("Clicked");
var Val=document.getElementById('email_id').value;
var Name=$(this).attr('name');
var Col=$(this).attr('id');
$.ajax({
url:"update.php",
method:"post",
data:{"Col":Col,"Name":Name, "Email":Val},
success:function(response){
console.log(response);
}
});
});

Related

Show value to Textarea with ajax is not working

I want to show value in textarea from a selected dropdown input field. The code is working fine with normal input field but i need the field as textarea and if change the field to textarea the code stops working.
<textarea id="mytext" class="form-control" style="height: 300px" name="text"></textarea>
$(document).ready(function(){
$('.productid').select2();
$(".productid").on('change', function(e){
var productid = this.value;
var tr=$(this).parent().parent();
$.ajax({
url:"getsignature.php",
method:"get",
data:{id:productid},
success:function(data){
tr.find ($('#mytext').val(data["signature"]));
}
})
})
})
Here #mytext is the id of my textarea.
i have text area with tinymce.in it
<script>
tinymce.init({
selector: '#mytext'
});
<textarea id="mytext" class="form-control" style="height: 300px" name="text"></textarea>
$(document).ready(function(){
$('.productid').select2();
$(".productid").on('change', function(e){
var productid = this.value;
var tr=$(this).parent().parent();
$.ajax({
url:"getsignature.php",
method:"get",
data:{id:productid},
success:function(data){
tr.find ($('#mytext').html(data["signature"]));
}
})
})
})
As you said your script is working fine on input field then now it should work fine on textarea as well.
To get value on textarea need to use html() instead of val().
Happy coding :)

ajax output in textbox is not available in post method of submit

I have a select dropdown, from that I'm calling an ajax function onchange event. Posting the result in a text input. This much is working perfectly. But when I submit the form the text input value is not submitting.
This is the ajax code. teachername dowpdown and teachercode text input
$("#teachername").change(function(){
var name = $(this).val();
var dataString = "name="+name;
$.ajax({
type: "POST",
url: "get-teachercode.php",
data: dataString,
success: function(result){
$("#teachercode").val(result);
}
});
});
this is the form
<form action="insert-assign-classes.php" method="post">
<select class="form-control" name="teachername" id="teachername" required>
</select>
<input type="text" disabled style="background-color:#fff;" class="form-control" name="teachercode" id="teachercode" required>
getting the value in insert-assign-classes.php file.
$teachercode= $_POST['teachercode'];//not working

PHP / MySQL / AJAX - Update multiple data

How to update multiple data using AJAX ?
Example :
TableA
id : 1, 2
name : Jack, John
It's only working with id 1, when I am trying to edit name for id 2 it's not working.
I have try with this code but failed.
HTML/PHP :
...
while($row=mysqli_fetch_array($query)){
echo'
<form class="btn-group">
<input type="text" class="form-control" name="id_user" id="id_user" data-user="'.$row['id'].'" value="'.$row['id'].'">
<input type="text" class="form-control" name="id_status" id="id_status" data-status="'.$row['id'].'" value="'.$row['id'].'">
<button type="submit" id="likestatus" class="btn btn-primary btn-outline btn-xs"><i class="fas fa-thumbs-up"></i></button>
</form>
';
}
AJAX :
$(document).ready(function(){
$("#likestatus").click(function(){
var id_user=$("#id_user").data("user");
var id_status=$("#id_status").data("status");
$.ajax({
url:'status/like-status.php',
method:'POST',
data:{
id_user:id_user,
id_status:id_status
},
success:function(response){
alert(response);
}
});
});
});
The problem with your code is that ids should be unique, but in the loop you create elements with same id.
Use this in the event handler to find the siblings of the button that has been clicked - closest returns the parent of type form.
$(document).ready(function(){
$(".btn-primary").click(function(){
var $form = $(this).closest('form');
var id_user=$form.find('[name="id_user"]').data("user");
var id_status=$form.find('[name="id_status"]').data("status");
$.ajax({
url:'status/like-status.php',
method:'POST',
data:{
id_user:id_user,
id_status:id_status
},
success:function(response){
alert(response);
}
});
});
});
You might want to use your own class instead of .btn-primary because this affects all buttons on the page.
Judging from the incomplete PHP, it appears as if you're not assigning to $ruser within your loop. This would mean you're always posting the same id to like-status.php.
PS: Would've posted as comment, but I can't.
Make your ID unique so make them dynamic
<?php
$counter = 0;
while($row=mysqli_fetch_array($query)){
$counter++;
echo'
<form class="btn-group">
<input type="text" class="form-control" id="userid_$counter" data-user="'.$ruser['id'].'" value="'.$ruser['id'].'">
<input type="text" class="form-control" name="id_status" id="status_$counter" data-status="'.$rtimeline['id'].'" value="'.$rtimeline['id'].'">
<button type="submit" id="likestatus_$counter" class="btn btn-primary btn-outline btn-xs"><i class="fas fa-thumbs-up"></i></button>
</form>
';
}
?>
Then
<script type="text/javascript">
$(document).ready(function(){
$('[id^="likestatus_"]').on('click',function(){
var index = $(this).attr('id').split("_")[1];
var id_user=$("#user_"+index).data("user");
var id_status=$("#status_"+index).data("status");
$.ajax({
url:'status/like-status.php',
method:'POST',
data:{
id_user:id_user,
id_status:id_status
},
success:function(response){
alert(response);
}
});
});
});
You're using the id's multiple times. Thus your query for var id_user=$("#id_user").data("user"); always finds the first input field on the page. You should avoid using the same id multiple times on one page (see this Question).
You may subscribe to the jQuery submit event of the form and then search for the input fields within that form, to properly extract the id_user and status_user values. For that you have to add an appropriate event listener to the <form> element. To find the form I would recommend adding a css-class like like-status-form.
$(document).ready(function(){
// We're attaching a submit-event listener to every element with the css class "like-status-form"
$(".like-status-form").submit(function(event){
// Form get's submitted
// Prevent that the Browser reloads the page
event.preventDefault();
// Extract the user id and status from the form element (=== $(this))
var id_user = $(this).find('[name="id_user"]').data('user');
var id_status = $(this).find('[name="id_status"]').data('status');
// TODO Perform AJAX Call here
});
});
To detect the form elements one can use the jQuery Attribute Equals Selector.
Find a working example at https://jsfiddle.net/07yzf8k1/

Ajax - Send PHP value via ajax to another page

i have a form with a switch toggle and a input field named userid.
When i switch the toggle all works fine and the active.php save the new status in the database.
But now i want to have also the userid value to the active.php.
The var mode can i get in the active.php via
$mode=$_POST['mode'];
How can i send the userid also to active.php?
Any idea?
Thank you
<input type="checkbox" name="<?php echo 'toggle'.$c;?>" id="<?php echo 'toggle'.$c;?>" data-toggle="toggle" data-off="OFF" data-on="ON" checked>
<input id="userid" name="userid" type="hidden" value="<?php echo $userid;?>">
This is the js code
$('#<?php echo 'toggle'.$c;?>').change(function(){
var mode= $(this).prop('checked'),
$.ajax({
type:'POST',
dataType:'JSON',
url:'active.php',
data:'mode='+mode,
success:function(data)
{
var data=eval(data);
message=data.message;
success=data.success;
$("#heading").html(success);
$("#body").html(message);
}
});
});
Use the & to separate the two keys.
First of all, retrieve the userid value
var userid = $('#userid').val();
data:'mode=' +mode+'&userid='+userid,
In your ajax parameter, change the data attribute to look as follows:
data: 'mode='+mode + '&userid=' + $('#userid').val ()

jQuery send post variable using ajax and submit form

I have html form with dynamical number of fields, for example:
<form id="myform">
<input type="text" id="input1">
<input type="text" id="input2">
<input type="text" id="input3">
...
<input type="text" id="inputN">
<span id="button_click"> CLICK </span>
</form>
and jQuery which is:
$("#button_click").click(function(){
$.post("myfile.php",
{
XXXX:YYYY
},
function(data,status){
// do anyting
});
});
I don't know the exact number of fields, so I can't fill XXXX - post variable name, and YYYY - field data from web page.... so I can't count/write one by one...
How can I submit whole form, through post variables, using AJAX and click button?
Sounds like you're looking for the .serialize() method:
$.post("myfile.php",
$("#myform").serialize(),
function(data,status){
// do anyting
});
http://api.jquery.com/serialize/
//rough code for general puporse of storing values for post
var obj = $("#myform"),
data = {};//to hold the values
obj.find('[name]').each(function(index, value) {
name = obj.attr('name'),
value = obj.val();
data[name] = value;
});
$.post("myfile.php",
data: data,
function(data,status){
// do anyting
});

Categories