pufos
pufos

Reputation: 2930

$_POST[] to hold arrays ... help

I want to append using jquery some <input> fields to a form like this

<form>
<input>
<input>
<input>
</form>

and when this form is processed i want to add multiple rows in database. for every field i want to add a row in database like this

('1','field_name');
('2', 'field_name');
('3', 'field_name');

How to manipulate the <input> to be processed in a $_POST['field'][0] , $_POST['field'][1] .. and so on ?

what is necesarry for the <input> tag to have as name ? <input name='field'> or <input name='field[]'> or if there's any other solution ... i heed some help . Thank you very much.

Upvotes: 2

Views: 82

Answers (2)

Treffynnon
Treffynnon

Reputation: 21553

You answered it yourself in the question:

<input name="field[]" />

This behaviour is documented in the PHP manual in PHP and HTML.

To do this with jquery you would do something like the following:

$('<input />', {
    "name": "field[]",
    "type": "text"
}).appendTo("body");

This is the way to create new elements in jQuery and add them to the body of the page.

To address concerns in your comments:

$('<input />', {
    "name": "field[]",
    "type": "text"
}).appendTo("#my-form-id");

Upvotes: 3

The name="field[]" solution will give you the desired behaviour. You can also define the order of the fields by

<input name="field[0]" />
<input name="field[1]" />
<input name="field[2]" />

But then make sure you don't have a double index.

Upvotes: 4

Related Questions