Hi all Im trying to pass form data and an additional variable using an AJAX call as seen below:
function tempFunction(obj) {
var data = $('form').serializeArray();
data.push(
{
no: $(obj).attr('id')
}
);
$.ajax({
type: "POST",
url: "/tempproject/main/changepage",
data: data,
success: function (msg) {
alert(msg);
}
});
}
however in PHP when I try and call back the 'no' variable I am getting an error saying undefined index. within my php file I am trying:
$test = $_POST['no'];
echo $test;
when clicking I receive a popup that says undefined index no.
Each element of the array is an object with a name and value property:
Try like this:
data.push({name: 'no', value: $(obj).attr('id')});
Datas should be passed within {}
like this in ajax
$.ajax({
type: "POST",
url: "/tempproject/main/changepage",
data: "{no: " + $(obj).attr('id') + "}",
success: function (msg) {
alert(msg);
}
});
this will keep your form values
function tempFunction(obj) {
var data = $('form').serialize();
data += '&no='.$(obj).attr('id');
$.ajax({
type: "POST",
url: "/tempproject/main/changepage",
data: data,
success: function (msg) {
alert(msg);
}
});
}