Saturday, October 30, 2010

PHP: How to use multiple combo boxes

At one point or another you will have to generate a separate combo box for each row retrieved from database or just a large sum of combo boxes in general. Creating unique variable names for each combo box is possible but a hassle and a waste of time. Therefor, I present to you an example on how to generate and process multiple combo boxes on the same page.

Here is the code, explanation is below it:

1. Create a form tag with an action to send the script back to the same page and method using POST.
2. Generate as many combo boxes as you like, this examples uses three.
3. Create your submit button and close form tag.
4. Check if submit button has been pressed and use '@' to ignore any errors raised by the statement. Because if the send button isn't pressed you'll get a warning saying $_POST['send'] is undefined or does not exist.
5. If submit test is evaluated to TRUE proceed with looping through array of combo boxes and print the value of each combo box. Here you can do whatever you want with your data.

Before pressing 'OK' each combo box was set to a selection in exactly the same order text is being printed below. Apples, oranges, and grape. Try it yourself.

No comments:

Post a Comment