i am trying to send multiple data using j query $.ajax method to my php script but i can pass only single data when i concatenate multiple data i get undefined index error i
You can create an object of key/value pairs and jQuery will do the rest for you:
$.ajax({
...
data : { foo : 'bar', bar : 'foo' },
...
});
This way the data will be properly encoded automatically. If you do want to concoct you own string then make sure to use encodeURIComponent()
: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent
Your current code is not working because the string is not concocted properly:
'id='+ id & 'name='+ name
should be:
'id='+ encodeURIComponent(id) + '&name='+ encodeURIComponent(name)
you can use FormData
take look at my snippet from MVC
var fd = new FormData();
fd.append("ProfilePicture", $("#mydropzone")[0].files[0]);// getting value from form feleds
d.append("id", @(((User) Session["User"]).ID));// getting value from session
$.ajax({
url: '@Url.Action("ChangeUserPicture", "User")',
dataType: "json",
data: fd,//here is your data
processData: false,
contentType: false,
type: 'post',
success: function(data) {},
var data = 'id='+ id & 'name='+ name;
The ampersand needs to be quoted as well:
var data = 'id='+ id + '&name='+ name;
var CommentData= "u_id=" + $(this).attr("u_id") + "&post_id=" + $(this).attr("p_id") + "&comment=" + $(this).val();
var value1=$("id1").val();
var value2=$("id2").val();
data:"{'data1':'"+value1+"','data2':'"+value2+"'}"
Change var data = 'id='+ id & 'name='+ name;
as below,
use this instead.....
var data = "id="+ id + "&name=" + name;
this will going to work fine:)