Dependent dropdown example in PHP, MySQL

This tutorial shows how to create dependent or cascaded dropdown in PHP and MYSQL. This tutorial uses JavaScript to submit the form automatically when first dropdown value is selected.

Sometimes we need to create such a cascaded dropdown when we need to select some value in the second dropdown based on first dropdown value or third dropdown value based on second dropdown value. For example, we need to select city dropdown value based on country dropdown value and for this we cannot pre-populate the city dropdown value unless we know the country from country dropdown.
I will explain here with an example. The example here contains two dropdown – one is parent category and another is sub-category. So depending on the parent category the sub-category dropdown gets populated.


MySQL database
PHP engine
Apache HTTP server

At the end of this example you will be able to see this output in the browser:

When the dependenddropdown.php file gets executed

Dependent dropdown in php and mysql

When you select the parent category and for this category if there is any sub-category

Dependent dropdown in php and mysql

When you select sub-category and press the submit button

Dependent dropdown in php and mysql

Read also Dependent Dropdown Example

Now look at the below source codes.

Create MySQL database table


Insert some data into the table


Please have a look for database connection tutorial.

Now we will see how it works in dependenddropdown.php file


Thanks for your reading. Please do not forget to leave a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *