Sam
Sam

Reputation: 333

serializeArray on keyless array input

If I have a form with a keyless array as one of the names like so:

<form id="form">
    <input name="array[]" type="text" value="zero" />
    <input name="array[]" type="text" value="one" />
    <input name="array[]" type="text" value="two" />
    <input name="array[]" type="text" value="three" />
    <input name="single" type="text" value="something" />
</form>

and I ultimately want to turn in into a multidimensional array in PHP like so:

Array ( [array] => Array ( [0] => zero [1] => one [2] => two [3] => three ) [single] => something )

How can I do that in Javascript/jQuery without POSTing the form data?

Note: I'll be sending whatever data I get with jQuery to PHP with Ajax and don't need help with that part. Really all I need is a method to package that form data in a way that I can eventually read with PHP. It doesn't seem like serializeArray() or serialize() can do the trick. Thanks in advance for your help.

Upvotes: 0

Views: 729

Answers (2)

rationalboss
rationalboss

Reputation: 5389

Something like this would work:

$.ajax({
    url: 'submit.php',
    data: $('#form').serialize()
});

or

$.post('submit.php',
    $('#form').serialize()
);

Upvotes: 0

Naftali
Naftali

Reputation: 146302

Use $('#form').serialize() along with $.post to accomplish what you need to do.

For example:

$.post(url, $("#form").serialize());

I am not sure how else you would post the form data without posting the form data!

Upvotes: 1

Related Questions