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!

2 Answers 2

5

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

<input type="text" name="child[]">
Sign up to request clarification or add additional context in comments.

Comments

1

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

HTML input - name vs. id

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.