Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create unique name for <input> in form

Okay, first thing, I had a lot of trouble thinking of a title for this, and also of what to search for in Google. So that may just be me being stupid, but here is what I would like you help with.

I have a form, that has a button that will add additional input fields, but I would like the the name of the field to iterate everytime the button is pressed. E.g. the first time it will be:

<input type="textfield" name="field1" value=""/>

Then the second time it is pressed, it will be:

<input type="textfield" name="field2" value=""/>

I also have a small example of what I currently have here: http://jsfiddle.net/5gh75/14/

Please let me know if you can help me, or if you require more info thanks :)

like image 599
Melloorr Avatar asked Apr 10 '26 07:04

Melloorr


1 Answers

The best way to handle this is to name them all field[].

When handled by the server-side code, it will build an array for you. For instance, in PHP you would get $_POST['field'][0], $_POST['field'][1] and so on.

like image 98
Niet the Dark Absol Avatar answered Apr 12 '26 22:04

Niet the Dark Absol