1

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?

2 Answers 2

1

You can use http://api.jquery.com/serialize/ to create a url encoded string from your checkboxes.

Sign up to request clarification or add additional context in comments.

Comments

0
$(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);
    });
  });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.