Page 1 of 1

Number of $_POST unknown due to dynamic form

Posted: Mon Feb 22, 2010 2:47 am
by mayo23
I am creating a reservation system for hotels, and am having a problem with one of the admin side forms.

This particular form allows the hotelier to allocate different rooms to a specific category (eg. Standard Double). The form has a number of checkboxes (one for each room in the hotel) and when the form is submitted, I would like to run multiple insert queries so that each room ticked will insert a different line into a single table.

I think the best way of achieving this part of the query would be to collect all the information and run a batch insert (please feel free to suggest better ways).

However, my problem is with sending the information through the form and out the other end.

The checkboxes are created dynamically and the number of boxes will depend on how many rooms the hotel has. Because of this, I cannot capture the POST at the other end, because I don't know how many checkboxes there will be until the form has been created, and the number of POSTs will vary form form to form.

Is there a way of capturing the POSTs without having to explicitly call each individual one?

Re: Number of $_POST unknown due to dynamic form

Posted: Mon Feb 22, 2010 10:07 am
by AbraCadaver
Set the check boxes up as an array and then loop through them. Short example:

Code: Select all

<input type="checkbox" name="rooms[]" value="100">
<input type="checkbox" name="rooms[]" value="101">
<input type="checkbox" name="rooms[]" value="102">

Code: Select all

foreach($_POST['rooms'] as $room) {
    // insert $room
}