Frequently Asked Questions on drop down list box using PHP MySQL

My first dropdown list is not populating with data
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
Page reloading is done by JavaScript function. This function is triggered by onchange event of the first drop down box. So check that you have written the trigger correctly and the function is there. Inside the function you have written the code correctly and the line self.location=dd.php?cat' + val ; is correctly replaced with your page name and variable name. As we are using JavaScript it should be enabled in your browser. Watch your browser address bar and it should read the page name with the value of the selection box name cat.

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 .

This 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.
Just ensure that all the other form components are within your form end tag. But here as the page is reloading we have to take care to retain the values of the other form components also. If the drop downs are the first to be filled then we can afford to keep other fields blank, but here if some one has already filed the lower part of the form first and the page reloads then the entered data for the other form components are lost. So inside the javascript form which triggers the page reloads we can keep all other form component values and pass them like category value back to the form.

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 ?
Yes this can be done. But first start with two drop downs and understands how they are linked and values are taken from one drop down to other. Once the page reloads the values are to be taken through query string ( at address bar ) so they are to be collected and formatted with the page name inside the JavaScript function. You can see the three drop down tutorial and demo. After a good understanding of this you can extend to more number of drop downs.
download script
Your Rating


Post Comment This is for short comments only. Use the forum for more discussions.

HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2017 All rights reserved worldwide Privacy Policy Disclaimer