Form with variable number of fields

I was thinking, what is the best way to manage a form with a variable number of fields?

Suppose you have the fields “phone1”, “phone2” and “phone3”, how can I insert the data into a database, not knowing how many fields I need to insert before the form gets submitted?

Question: Do you need to be able to search on said data?

In which sense?

If I understood what you meant then no, data would just be inserted in a database table.

per your example, rather than phone1, phone2 etc, create them as an array phone. then you can loop thru then when processing

You mean that the field name should be “phone”?

<input type="text" name="phone[]" />
<input type="text" name="phone[]" />
<input type="text" name="phone[]" />

Which then returns all phones in an array ($_POST[‘phone’][0], etc). You could save them in a single database field by imploding them on an unused character (something other than a number) after sanitizing them.

Could you please explain the section in bold?

imploding the entries of the array makes it a single string with a given delimiter. single string = single field, no matter how many phone numbers there are.

Ok, thanks :slight_smile: