There are many situation where you will need to dynamic dropdown like you have a dropdown which contains countries name and you have to find all state of selected country in select box or dropdown. Here we are going to tell you how to make dynamic select box using jQuery, Ajax and PHP. Firstly we will fetch all country from country table then we will use Ajax and jQuery to fetch all state of selected country. After getting all state we will append to all state on state dropdown.

Here we are using MySQLi as a database which contains two table country and state. For making dynamic dependent select box use following steps as shown below:

1.  Create database Discussdesk from phpmydmin and can also use following sql query to create it as shown below:

These following above query will create two table. To insert data in these table you can get data by clicking download button or import all table with data. We are attaching discussdesk.sql file to import all table with data in discussdesk database.

3.  Create db_config.php file with following code as shown below and put your database name what you made from phpmyadmin.

4.  Create Second file country.php in which we fetch all countries from database and append in select box. Here we fire function on onchange event of country select box in which using Ajax it will go on state.php page with country id to fetch all record from states table and return in Ajax response. After getting response in success function we append response to state dropdown box basis of id. The code of country.php is as follows:

5.  Create another file state.php file as shown below. This file fetch all record from states table basis of country_id column and return all record to Ajax success function.

I hope this article will help to make two Dynamic Dependent Select Box. Still you have any query then feel free to comment below.