I’m using $.ajax to send the values of a form to a PHP script.
The form input fields are read like arrays by PHP, but I don’t know how to do the same with javascript and jQuery, and send them in such a way that PHP is able to read them like arrays.
For example a input named foo[bar] in PHP you get it as $_POST['foo']['bar']
Currently I’m sending this data like
data:{
foo_bar: $('form').find('#foo_bar').val(),
foo_xxx: $('form').find('#foo_xx').val()
},
and manually assembling the array in the PHP script.
But it’s starting to take a lot of lines of code.
Could I somehow automate this, and send all the form input as an multidimensional array to PHP?
You could use $.serialize();
Check for more information here: http://api.jquery.com/serialize/
It will send back a string
field=value&field2=value&and you could useparse_strto handle itparse_str sample:
Results to: