Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to jQuery ajax with input field name="array[]"?

I have several dynamically created hidden input fields. Most of which have a name formatted as array[]

Question 1:

How can I use jQuery .ajax() or .post() to get the values from every field named array[] and pass them so they'll be retrievable as $_POST['array'] in my PHP page?

Question 2:

Hypothetically speaking. Let's say that I don't know the name of said field but only the name of the form. How can I still do the same thing as in Question 1?

I found .serializeArray() in the jQuery documentation, but I have no idea what I'm doing with that and I'm not even certain if that applies to my situation of not knowing the field names.

Thanks in advance.

like image 702
Vitaliy Isikov Avatar asked Jan 24 '11 21:01

Vitaliy Isikov


2 Answers

You want to use .serialize() on the form. This will make a query string of all form elements (including 'name[]' ones).

$.post('/url/to/post', $('#form').serialize(), function(data){
   alert('POSTed');
});
like image 110
Rocket Hazmat Avatar answered Oct 11 '22 19:10

Rocket Hazmat


You'll want to use jQuery's .serialize() method.
Check it out

like image 40
Aaron Hathaway Avatar answered Oct 11 '22 19:10

Aaron Hathaway