title: Dependent dropdown list in PHP from same table of the database
body: <p>I need to make some dpendent dropdown lists. Here, from first dropdown list, whenever I select <strong>Region</strong>, in second dropdown list's option, it should show corresponding <strong>Country</strong> of the selected <em>Region</em>. But after selecting a <em>Region</em>, second dropdown list doesn't show anything. I searched for solution, but didn't find appropriate one as I'm doing all this for only one table of database.</p>
  
  <p><strong>Database</strong><br></p>
  
  <pre><code>SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
  SET time_zone = "+00:00";
  
  CREATE TABLE `geo` (
    `Region` varchar(200) NOT NULL,
    `Country` varchar(200) NOT NULL,
    `City` varchar(200) NOT NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  
  INSERT INTO `geo` (`Region`, `Country`, `City`) VALUES
  ('Asia', 'Bangladesh', 'Dhaka'),
  ('Europe', 'France', 'Paris'),
  ('Asia', 'Bangladesh', 'Khulna'),
  ('Europe', 'France', 'Avignon'),
  ('Europe', 'Spain', 'Barcelona'),
  ('Europe', 'Spain', 'Madrid'),
  ('Asia', 'Srilanka', 'Colombo');
  </code></pre>
  
  <p><strong>dbconnect,php</strong></p>
  
  <pre><code>&lt;?php
  error_reporting( ~E_DEPRECATED &amp; ~E_NOTICE );
  
  define('DBHOST', 'localhost');
  define('DBUSER', 'root');
  define('DBPASS', '');
  define('DBNAME', 'ddl');
  
  $conn = mysql_connect(DBHOST,DBUSER,DBPASS);
  $dbcon = mysql_select_db(DBNAME);
  
  if ( !$conn ) {
      die("Connection failed : " . mysql_error());
  }
  
  if ( !$dbcon ) {
      die("Database Connection failed : " . mysql_error());
  }
  
  ?&gt;
  </code></pre>
  
  <p><strong>index.php</strong></p>
  
  <pre><code>&lt;?php
  require_once 'dbconnect.php';
  
  $query ="SELECT * FROM geo";
  $results = mysql_query($query);
  ?&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;DDL - Select&lt;/title&gt;
  &lt;/head&gt;&lt;?php 
          $bul[10000007] = false;
      ?&gt;
  
  &lt;style&gt;
  body{width:610px;}
  .frmDronpDown {border: 1px solid #F0F0F0;background-color:#C8EEFD;margin: 2px       0px;padding:40px;}
  .demoInputBox {padding: 10px;border: #F0F0F0 1px solid;border-radius:  4px;background-color: #FFF;width: 50%;}
  .row{padding-bottom:15px;}
  &lt;/style&gt;
  &lt;script src="https://code.jquery.com/jquery-2.1.1.min.js"   type="text/javascript"&gt;&lt;/script&gt;
  &lt;script&gt;
  function getCountry(val) {
  $.ajax({
      type: "POST",
      url: "get_country.php",
      data:'region_id='+val,
  
     success: function(data){
      $("#country-list").html(data);
    }
    });
    }
  
    function selectRegion(val) {
    $("#search-box").val(val); 
    $("#suggesstion-box").hide();
    }
    &lt;/script&gt;
  
   &lt;body&gt;
  &lt;div class="frmDronpDown"&gt;
  &lt;div class="row"&gt;
  
  &lt;form&gt;
  &lt;label&gt;Region: &lt;/label&gt;
  &lt;select name="region" id="region-list" class="demoInputBox"      onChange="getCountry(this.value);"&gt;
      &lt;option value=""&gt;Select Region&lt;/option&gt;
  
          &lt;?php
  
          $sql = "SELECT * FROM geo";
          $res = mysql_query($sql);
          while($row = mysql_fetch_assoc($res)) {
  
              if($bul[$row['Region']] != true &amp;&amp; $row['Region'] != 'Region'){
              ?&gt;
                  &lt;option value="&lt;?php echo $row['Region']; ?&gt;"&gt;&lt;?php echo   $row['Region']; ?&gt;&lt;/option&gt;
          &lt;?php   
                  $bul[$row['Region']] = true;
              }
          } 
          ?&gt;
  
      &lt;/select&gt;
  &lt;/div&gt;
  
  
  &lt;div class="row"&gt;
      &lt;label&gt;Country: &lt;/label&gt;
      &lt;select name="country" id="country-list" class="demoInputBox"&gt;
          &lt;option value=""&gt;Select Country&lt;/option&gt;
      &lt;/select&gt;   
  
  
  &lt;/form&gt;
  
  &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>get_country.php</strong></p>
  
  <pre><code>&lt;?php
      require_once 'dbconnect.php';
  
  
      if(!empty($_REQUEST["region_id"])) {
  
      $query ="SELECT * FROM geo WHERE Region =" . $_POST["region_id"];
      $result = mysql_query($query);
  ?&gt;
      &lt;option value=""&gt;Select Country&lt;/option&gt;
  
  &lt;?php
      while($row2=mysql_fetch_assoc($result)){
          if($bul2[$row2['Country']] != true &amp;&amp; $row2['Country'] != 'Country')        { ?&gt;
              &lt;option value="&lt;?php echo $row['Country']; ?&gt;"&gt;&lt;?php echo     $row['Country']; ?&gt;  &lt;/option&gt;
   &lt;?php  
           $bul2[$row2['Country']] = true;
           }
       }
      }
  ?&gt;
  </code></pre>