title: how to auto complete cities list based on entered state name in PHP MySQL and JQuery
body: <p>I am able to auto fill(complete) states and cities textboxes independently using below mentioned script. </p>
  
  <p>But I want to do following.</p>
  
  <ul>
  <li><p>First I enter state name in state textbox and select any state from suggested states list.</p></li>
  <li><p>There after I enter city name in city textbox but at that time suggested cities list should be filtered according to selected state. </p></li>
  <li><p>For Example: First I enter/select 'New York' in state textbox, after that when I enter city name in city textbox then I should get suggest cities list only for 'New York' state.</p></li>
  </ul>
  
  <p>Please let me know what changes I need to do in script to get desired result.</p>
  
  <p>NOTE: statepkid field is available in tr_cities table for relation.</p>
  
  <p>index.php</p>
  
  <pre><code>&lt;link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"&gt;
  &lt;script src="//code.jquery.com/jquery-1.10.2.js"&gt;&lt;/script&gt;
  &lt;script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"&gt;&lt;/script&gt;
  &lt;script&gt;
      $(function() {
          $( "#statename" ).autocomplete({
            source: 'search.php'
          });
      });
      $(function() {
          $( "#cityname" ).autocomplete({
            source: 'c_search.php'
          });
      });
  &lt;/script&gt;
  
  &lt;div class="ui-widget"&gt;
      &lt;label for="statename"&gt;State: &lt;/label&gt;
      &lt;input id="statename"&gt;
  
      &lt;label for="cityname"&gt;City: &lt;/label&gt;
      &lt;input id="cityname"&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>search.php (for states)</p>
  
  <pre><code>$dbHost = 'localhost';
  $dbUsername = 'root';
  $dbPassword = 'password';
  $dbName = '10000dbname';
  $db = new mysqli($dbHost,$dbUsername,$dbPassword,$dbName);
  $searchTerm = $_GET['term'];
  $query = $db-&gt;query("SELECT * FROM tr_states WHERE statename LIKE '%".$searchTerm."%' ORDER BY statename ASC");
  while ($row = $query-&gt;fetch_assoc()) {
      $data[] = $row['statename'];
  }
  echo json_encode($data);
  </code></pre>
  
  <p>c_search.php (for cities)</p>
  
  <pre><code>$dbHost = 'localhost';
  $dbUsername = 'root';
  $dbPassword = 'password';
  $dbName = '10000dbname';
  $db = new mysqli($dbHost,$dbUsername,$dbPassword,$dbName);
  $searchTerm = $_GET['term'];
  $query = $db-&gt;query("SELECT * FROM tr_cities WHERE city LIKE '%".$searchTerm."%' ORDER BY city ASC");
  while ($row = $query-&gt;fetch_assoc()) {
      $data[] = $row['city'];
  }
  echo json_encode($data);
  </code></pre>