title: How to keep the selected onchange dropdown list value after submit
body: <p>I have three dropdown list which is country,state and city. At first, the country dropdown would be displayed with all countries. When a country would be chosen, the respective states would be fetched from the MySQL database and appear in the states dropdown. Alike when a state would be chosen, the respective cities will be fetched from the MySQL database and appear in the cities dropdown. </p>
  
  <p>Below is the default display before I select country,state,city and click submit button.</p>
  
  <p><a href="https://i.stack.imgur.com/GTm2U.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GTm2U.png" alt="enter image description here"></a></p>
  
  <p>After I select country, state, city and click submit button like below. It will refresh and go back to the default display.</p>
  
  <p><a href="https://i.stack.imgur.com/k9WNB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/k9WNB.png" alt="enter image description here"></a></p>
  
  <p>So how can I keep the selected value(United Kingdom,England,London) display in the dropdown list instead it jump back to default display after clicked submit button?</p>
  
  <p>Index.php</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;style type="text/css"&gt;
  .select-boxes{width: 280px;text-align: center;}
  
  &lt;/style&gt;
  &lt;script src="jquery.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  $(document).ready(function(){
  $('#country').on('change',function(){
      var countryID = $(this).val();
      if(countryID){
          $.ajax({
              type:'POST',
              url:'ajaxData.php',
              data:'country_id='+countryID,
              success:function(html){
                  $('#state').html(html);
                  $('#city').html('&lt;option value=""&gt;Select state first&lt;/option&gt;'); 
              }
          }); 
      }else{
          $('#state').html('&lt;option value=""&gt;Select country first&lt;/option&gt;');
          $('#city').html('&lt;option value=""&gt;Select state first&lt;/option&gt;'); 
      }
  });
  
  $('#state').on('change',function(){
      var stateID = $(this).val();
      if(stateID){
          $.ajax({
              type:'POST',
              url:'ajaxData.php',
              data:'state_id='+stateID,
              success:function(html){
                  $('#city').html(html);
              }
          }); 
      }else{
          $('#city').html('&lt;option value=""&gt;Select state first&lt;/option&gt;'); 
      }
  });
  });
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form id="form1" name="form1" method="get" action="&lt;?php echo     $_SERVER['PHP_SELF'];?&gt;"&gt;
  &lt;?php
  //Include database configuration file
  include('dbConfig.php');
  
  //Get all country data
  $query = $db-&gt;query("SELECT * FROM countries WHERE status = 1 ORDER BY country_name ASC");
  
  //Count total number of rows
  $rowCount = $query-&gt;num_rows;
  ?&gt;
  &lt;select  name="country" id="country"&gt;
      &lt;option value=""&gt;Select Country&lt;/option&gt;
      &lt;?php
      if($rowCount &gt; 0){
          while($row = $query-&gt;fetch_assoc()){ 
              echo '&lt;option value="'.$row['country_id'].'"&gt;'.$row['country_name'].'&lt;/option&gt;';
          }
      }else{
          echo '&lt;option value=""&gt;Country not available&lt;/option&gt;';
      }
      ?&gt;
  &lt;/select&gt;
  
  &lt;select  name="state" id="state"&gt;
      &lt;option value=""&gt;Select country first&lt;/option&gt;
  &lt;/select&gt;
  
  &lt;select  name="city" id="city"&gt;
      &lt;option value=""&gt;Select state first&lt;/option&gt;
  &lt;/select&gt;
  &lt;input type="submit" name="Submit" id="Submit" value="Submit" /&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>ajaxData.php</p>
  
  <pre><code>&lt;?php
  //Include database configuration file
  include('dbConfig.php');
  
  if(isset($_POST["country_id"]) &amp;&amp; !empty($_POST["country_id"])){
  //Get all state data
  
  $query = $db-&gt;query("SELECT * FROM states WHERE country_id IN (".$_POST['country_id'].")");
  
  //Count total number of rows
  $rowCount = $query-&gt;num_rows;
  
  //Display states list
  if($rowCount &gt; 0){
      echo '&lt;option value=""&gt;Select state&lt;/option&gt;';
      while($row = $query-&gt;fetch_assoc()){ 
          echo '&lt;option value="'.$row['state_id'].'"&gt;'.$row['state_name'].'&lt;/option&gt;';
      }
  }else{
      echo '&lt;option value=""&gt;State not available&lt;/option&gt;';
  }
  }
  
  if(isset($_POST["state_id"]) &amp;&amp; !empty($_POST["state_id"])){
  //Get all city data
  $query = $db-&gt;query("SELECT * FROM cities WHERE state_id IN(".$_POST["state_id"].")");
  
  //Count total number of rows
  $rowCount = $query-&gt;num_rows;
  
  //Display cities list
  if($rowCount &gt; 0){
      echo '&lt;option value=""&gt;Select city&lt;/option&gt;';
      while($row = $query-&gt;fetch_assoc()){ 
          echo '&lt;option value="'.$row['city_id'].'"&gt;'.$row['city_name'].'&lt;/option&gt;';
      }
  }else{
      echo '&lt;option value=""&gt;City not available&lt;/option&gt;';
  }
  }
  ?&gt;
  </code></pre>
  
  <p>dbConfig.php</p>
  
  <pre><code>&lt;?php
  //db details
  $dbHost = 'localhost';
  $dbUsername = 'root';
  $dbPassword = '';
  $dbName = 'location_db';
  
  //Connect and select the database
  $db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
  
  if ($db-&gt;connect_error) {
  die("Connection failed: " . $db-&gt;connect_error);
  }
  ?&gt;
  </code></pre>