Number of $_POST unknown due to dynamic form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mayo23
Forum Newbie
Posts: 4
Joined: Thu Nov 22, 2007 3:52 pm

Number of $_POST unknown due to dynamic form

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Number of $_POST unknown due to dynamic form

Post 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
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply