How to Create Dynamic Select Box using PHP

Dynamic Select Box using PHP

In this tutorial, we are going to learn on how to create Dynamic Select Box using PHP. This tutorial works by the user for selecting the first select box which is a list of country, then the second select box will automatically load without any refreshing the whole page and it will display the corresponding states in the chosen country and the same with the third select box it will display cities in the chosen states.

For example

The selected country is Australia. The corresponding states are Victoria, Tasmania, South Australia, Western Australia, Queensland, and New South Wales. And it will correspond also the Cities of the selected states. These are Melbourne, Geelong, Ballarat, and Bendigo. You can also check the live demo of this simple tutorial, so you can get an idea and you can try this out, let's start coding.

Creating our Table

We are going to make our database. To create a database:
  1. Open the PHPMyAdmin.
  2. Create a database and name it as "db_countries".
  3. After creating a database name, then we are going to create our table. And name it as "tbl_state", "tbl_city", "tbl_country".
  4. Kindly copy the code below.
  1. -- --------------------------------------------------------
  2.  
  3. --
  4. -- Table structure for table `tbl_country`
  5. --
  6.  
  7. CREATE TABLE `tbl_country` (
  8.   `country_id` INT(11) NOT NULL,
  9.   `country_name` VARCHAR(100) NOT NULL
  10. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  1. -- --------------------------------------------------------
  2.  
  3. --
  4. -- Table structure for table `tbl_state`
  5. --
  6.  
  7. CREATE TABLE `tbl_state` (
  8.   `state_id` INT(11) NOT NULL,
  9.   `country_id` INT(11) NOT NULL,
  10.   `state_name` VARCHAR(100) NOT NULL
  11. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  1. -- --------------------------------------------------------
  2.  
  3. --
  4. -- Table structure for table `tbl_city`
  5. --
  6.  
  7. CREATE TABLE `tbl_city` (
  8.   `city_id` INT(11) NOT NULL,
  9.   `state_id` INT(11) NOT NULL,
  10.   `city_name` VARCHAR(100) NOT NULL
  11. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Markup Source Code

It contains 3 select boxes for the user to select the country, state, and city.
  1. <div>
  2. <label>Country :</label>
  3. <select name="country" class="country">
  4. <option selected="selected">--Select Country--</option>
  5. <?php
  6.         $stmt = $conn->prepare("SELECT * FROM tbl_country");
  7.         $stmt->execute();
  8.         while($row=$stmt->fetch(PDO::FETCH_ASSOC))
  9.         {
  10.                 ?>
  11.         <option value="<?php echo $row['country_id']; ?>"><?php echo $row['country_name']; ?></option>
  12.         <?php
  13.         }
  14. ?>
  15.  
  16. <label>State :</label>
  17. <select name="state" class="state">
  18. <option selected="selected">--Select State--</option>
  19.  
  20. <label>City :</label>
  21. <select name="city" class="city">
  22. <option selected="selected">--Select City--</option>
  23.  
  24. </div>
  25. <br />

Script Source Code

Using this script will help us to become dynamic our select box in the web page.
  1. <script type="text/javascript">
  2. $(document).ready(function()
  3. {
  4.         $(".country").change(function()
  5.         {
  6.                 var id=$(this).val();
  7.                 var dataString = 'id='+ id;
  8.                 $.ajax
  9.                 ({
  10.                         type: "POST",
  11.                         url: "get_state.php",
  12.                         data: dataString,
  13.                         cache: false,
  14.                         success: function(html)
  15.                         {
  16.                                 $(".state").html(html);
  17.                         }
  18.                 });
  19.         });
  20.         $(".state").change(function()
  21.         {
  22.                 var id=$(this).val();
  23.                 var dataString = 'id='+ id;
  24.                 $.ajax
  25.                 ({
  26.                         type: "POST",
  27.                         url: "get_city.php",
  28.                         data: dataString,
  29.                         cache: false,
  30.                         success: function(html)
  31.                         {
  32.                                 $(".city").html(html);
  33.                         }
  34.                 });
  35.         });
  36. });
  37. </script>

Output:

Selected Country Result Selected States Result Selected City Result Thank you for reading this tutorial. So, this is it, or you can download the full source code below by clicking the "Download Code" button below. Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment