0

I want to send an array to PHP through Ajax.

array = $('.def-mask :checkbox:checked').serialize();
$.ajax({
    url: 'ajax/battle.php',
    type: 'post',
    data: { playerReady: 1, attack: attack, defence: array },
    success: function(data) {
        alert(data);
    }
});

But when I do var_dump($_POST['defence']), I get string(), and not array(). Why do I get a string, and not an array?

9
  • you need to json_decode(array)..?? Commented Jun 2, 2013 at 8:56
  • Have you maybe forgotten to append [] to the name of your checkboxes? I don't know if jQuery deals with that automatically, I think it does... Commented Jun 2, 2013 at 8:57
  • Do console.log(decodeURIComponent($.param({ playerReady: 1, attack: attack, defence: array })); and see how your data gets actually formatted. Commented Jun 2, 2013 at 8:59
  • @pbibergal: The data is sent in the body of the request, not the header, and of course every request only contains of character data. But json_decode wouldn't help because the data is not formatted as JSON. Commented Jun 2, 2013 at 9:01
  • 1
    What is your actual var_dump of $_POST? Commented Jun 2, 2013 at 9:08

1 Answer 1

1

use .serializeArray() instead of .serialize() refer here

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

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.