I have a page which enables someone to book a lesson for a group. The person booking needs to say how many people will be in the group and what their names/emails are. It could be one; it could be ten.
You could probably use some JavaScript to create the multiple forms, name the fields using array names (input type="text" name="name[]" for example) and it would be relatively easy to process them without knowing how many forms are actually there. Of course, your JavaScript could also populate a hidden form variable to say how many fields there are, if you prefer.
An alternate might be to just present the one form for one name/address, and each time the user submits the form, add that name and email to a temporary table. Each time the form is presented, you have an “add user” button to add the form contents to the list, and a “list complete” button which then submits all the names on your temporary list to the next stage of the booking.
I’d be happy to help, but my JS is almost non-existent - I can look at it, perhaps spot typos, and sometimes build bits of it up into something that works, but no more than that. Post in the JS section, though, and there are many helpful people in there who actually know what they’re talking about.
How dynamically added form fields are normally done, is you output one initial set of form field(s), with an add/+ field button after it (javascript is used to append a copy of the html of the initial set of fields, to the form.) You would fill in/select the data for the 1st set of field(s), then click the add/+ button for that field set to append a new set of field(s), with its own add/+ button (and usually a remove/- button.) Repeat until all the data has been filled in/selected. Then, submit the form. As @benanamen wrote in the other thread, there are countless examples of doing this posted on the web.
Is the operation you have described, where the user initially selects the number of field sets, your preferred method of operation? If so, you would have an on-change event attached to that select/option menu. The javascript would get the selected option value, minus one, and use that in a loop to control how many copies of the initial set of form field(s) to append to the form.
Since you should be selecting from existing (registered) students at the point of picking them for a specific group, the available students should already be stored in a database table. The User Interface (UI) for this operation should be selecting from existing students (either using a select/option menu, or check-boxes), probably matching some filter/search term, such as a grade level or those taking a class from a specific teacher, not typing in values, and the form would only submit the selected student ids (auto-increment primary index), not two different pieces of data for each student.
Hi @mike324, rather than adding multiple forms I’d suggest to just append fieldsets to the parent form, which would be easier to process – you’d just have to submit that single form. Such a fieldset can be defined as a template: