Frequently Asked Questions on drop down list box using PHP MySQL
Check your database connection. Without executing the drop down script first check your database connection string. Only execute the database connection and see that without any error you are able to connect and collect data from your tables. If want to learn how to collect data from a table and populate the drop down list then read this article. See that you are getting all the records and your SQL query is fine. It is always a good idea to print the sql error messages returned by MySQL to know what is wrong.
On selection of the first list box my page is not reloading
My page is reloading but second list box is not displaying the matching option
First check the address bar and see that your first drop down select box name with value is there. In this tutorial the name given to first category is cat. So cat should have some value. Like this .
http://www.plus2net.com/php_tutorial/dd.php?cat=1This value of cat is available now for our second drop down list box to use after the page reloads. Here to collect the value of cat from address bar we have to use like this.
Now value of $cat is available and it is a good idea to test the value of $cat by giving a print command to check the value. Now once the value of cat is available it should go to the second drop down box query. If you have doubt then print the second query to screen and see how it is constructed. It is always a good idea to print the MySQL error message to screen after the mysql_query() command.
Once my page reloads, my first drop down list is not holding the selected options.
While adding options to the first drop down list box there is an if condition checked for each record to know if they are matching with first category selection name cat. If they are equal then to the option selected is added. This way the first drop down box holds the value of the selection once the page reloads. For more details on this technique you can read the tutorial on how to hold the selected option once the page reloads.
I want to include this along with my other form field components.
If you are submitting the form with drop down list boxes to another page and using another form with other details to be filled then use hidden tags to keep the drop down selected values in second form.
Can I use more than two drop downs and link them ?