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

10,730

Solution 1

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');
});

Solution 2

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

Share:
10,730
Vitaliy Isikov
Author by

Vitaliy Isikov

Updated on June 16, 2022

Comments

  • Vitaliy Isikov
    Vitaliy Isikov almost 2 years

    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.