title: Drop down with MySQL and PHP sample
body: <p>I have tried out this example but not sure where I have gone wrong - I've created the database and relative fields. When I select UK it gives me a blank result. Here is my script </p>
  
  <p>main.php</p>
  
  <pre><code>&lt;?php
  $connect=mysql_connect("localhost","root","") or die("Cannot connect");
  $dbselect=mysql_select_db("test");
  $sql_country = "SELECT * FROM COUNTRY";
  $result_country = mysql_query($sql_country) or die("Query problem");
  
  echo "&lt;form method='POST'&gt;";
  echo "&lt;select name='country' onChange='get_cities(this.value)'&gt;"; //get_cities is defined below
  echo "&lt;option value=''&gt;&lt;/option&gt;";
  while($row_country = mysql_fetch_array($result_country))
  {
  echo "&lt;option value='".$row_country['id']."'&gt;".$row_country['country']."&lt;/option&gt;";
  }
  echo "&lt;/select&gt;";
  
  echo "&lt;select name='city' id='city'&gt;&lt;/select&gt;"; //We have given id to this dropdown
  echo "&lt;/form&gt;";
  ?&gt;
  &lt;script type="text/javascript"&gt;
  function get_cities(country_id)
  {
  $.ajax({
  type: "POST",
  url: "cities.php", /* The country id will be sent to this file */
  beforeSend: function () {
  $("#city").html("&lt;option&gt;Loading ...&lt;/option&gt;");
  },
  data: "country_id="+country_id,
  success: function(msg){
  $("#city").html(msg);
  }
  });
  }
  &lt;/script&gt;
  </code></pre>
  
  <p>cities.php</p>
  
  <pre><code>&lt;?php
  // Code for cities.php
  $connect=mysql_connect("localhost","root","") or die("Cannot connect");
  $dbselect=mysql_select_db("test");
  $country_id = $_REQUEST['country_id'];
  
  $sql_city = "SELECT * FROM CITY WHERE country_id = '".$country_id."'";
  $result_city = mysql_query($sql_city);
  echo "&lt;select name='city'&gt;";
  
  while($row_city = mysql_fetch_array($result_city))
  {
  echo "&lt;option value='".$row_city['id']."'&gt;".$row_city['city']."&lt;/option&gt;";
  }
  
  echo "&lt;/select&gt;";
  
  ?&gt;
  </code></pre>
  
  <p>Sample code: <a href="http://www.x-developer.com/php-scripts/loading-drop-downs-with-ajax-php-and-fetching-values-from-database-without-refreshing-the-page" rel="nofollow">http://www.x-developer.com/php-scripts/loading-drop-downs-with-ajax-php-and-fetching-values-from-database-without-refreshing-the-page</a></p>