Reputation: 2863
I have a Checkbox array check[] and I need to pass this via a JQuery Ajax request to another php file. I would like the receiving php file to pick up the array as it would any other POST variable and the way it would without Ajax as $_POST['check'].
Do I need to use JQuery to parse this data into an array?
Upvotes: 1
Views: 3003
Reputation: 7388
$(document).ready(function(){
//when the form is submitted
$("form").submit(function(e){
//prevent the form from actually submitting.
e.preventDefault();
//put your PHP URL in here..
var url = "postToThisURL";
//create empty object
var obj = {};
//grab the checkboxes and put in arr
var arr = $(this).serializeArray();
//iterate over the array and change it into an object
for (var i = 0; i < arr.length; ++i) obj[arr[i].name] = arr[i].value;
//post the data to the server
$.post(url, obj, function(r){
//log the response when it is complete.
console.log(r);
});
});
});
Upvotes: 0
Reputation: 36
You can use http://api.jquery.com/serialize/ to create a url encoded string from your checkboxes.
Upvotes: 1