4

Let's say I've got a form in which you can describe a family:

<input type="text" name="mother">
<input type="text" name="father">
<input type="text" name="child">

But I want people to be able to add an infinite number of children to the form. So I can use Javascript to simply continue adding new inputs for more children, but I wonder how I should name these. Do I simply do child1, child2, child3, etc.? And on the backend I simply loop over all the children?

And what if all children also need an "age" input field. Do I simply continue adding fields such as child1_age, child2_age, child3_age?

I guess this would work, but I have the feeling that there's a smarter way of this. Does anybody know of a better way to handle this? All tips are welcome!

kramer65
  • 39,074
  • 90
  • 255
  • 436

2 Answers2

5

HTML forms can use arrays as input names. For example:

<input type="text" name="child[]">
Brett DeWoody
  • 50,328
  • 25
  • 121
  • 168
1

Have a look at this discussion. Introduce the id attribute and keep the name attribute constant.

HTML input - name vs. id

Community
  • 1
  • 1
Diversity
  • 1,870
  • 10
  • 20