Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to jQuery's serialize()?

I have a lot of inputs with the same name attribute. I know how to get them all as jQuery object!

The console.log() prints-out something like this:

[
<input type=​"checkbox" name=​"people" value=​"33" checked=​"checked">​
, 
<input type=​"checkbox" name=​"people" value=​"1" checked=​"checked">​
]

After that, I need to send an AJAX request to server that contains all IDs of people (value attribute).

Obviously, I can chain serialize() to that jQuery object and the result will be like:

people=33&people=1

...but I'm looking for 'more-friendly' way. 'More-friendly' to PHP, so I can do like:

foreach ($_POST['people'] as $people) {

    echo $people;

}
like image 374
daGrevis Avatar asked Dec 22 '22 08:12

daGrevis


2 Answers

PHP requires multiple checkboxes of the same name to use bracket notation. Using the following naming convention should allow you to do what you want.

<input type=​"checkbox" name=​"people[]" value=​"1" checked=​"checked">​
like image 65
Jason McCreary Avatar answered Dec 24 '22 01:12

Jason McCreary


.serializeArray() might do the trick: http://api.jquery.com/serializeArray/

like image 21
bstakes Avatar answered Dec 24 '22 02:12

bstakes