How to fetch serialized jquery array to a php page?
heres my code ive serialized checboxes and i want it to be pass to a php page through ajax. NOTE that i am not using forms. and i cant use forms. i am using click event on a button to pass the value.
$('#res-button').click(function (){
var pid = $('#pid');
var name_res= $('#name_res');
var email_res= $('#email_res');
var contact_res= $('#contact_res');
var room_id=$('[name=rescheck[]]:checked').serialize().replace(/%5B%5D/g,'[]');
alert(room_id);
$.ajax({
type: "POST",
url: "reservation-valid.php",
data: {name_r:name_r, email_r:email_r,contact_r:contact_r,prop_id:p_id,cvalue:room_id},
success: function(data) {
//some codes here
});
}
});
});
THE RESULT OF MY ALERT TO room_id is this- "rescheck[]=1&rescheck[]=2&....." so i know my serialize is working, but i cant get it to php to insert my ids in database.
this is my php code:
$c_array=$_POST['cvalue'];
foreach($c_array as $ch)
{
$sql=mysql_query("INSERT INTO reservation VALUES('','$prop_id','$ch','$name_r','$contact_r','$email_r','')");
}开发者_开发百科
change your js:
$.ajax({
type: "POST",
url: "reservation-valid.php",
data: {'name_r':name_r, 'email_r':email_r,'contact_r':contact_r,'prop_id':p_id,'cvalue':room_id},
success: function(data) {
//some codes here
});
}
});
your names of the values are being overrided
I suggest you to use a JSON approach using serializeArray
in php you should use JSON_DECODE to receive the data from the $.ajax call
pd: Sorry for my english.
精彩评论