title: Cascading jQuery Dropdowns with JSON and Multiple MySQL Tables using PHP
body: <p>I'm fairly new to PHP and MySQL, have a little experience with jQuery and almost no experience with JSON, just to give you some background. I am trying to implement cascading dropdowns in my form.</p>
  
  <p>I have two tables:</p>
  
  <pre><code>|city|
  |city_id INT| - PK
  |city VARCHAR (45)|
  |state_state_id INT | - FK
  
  |state|
  |state_id INT| - PK
  |state VARCHAR (25)|
  </code></pre>
  
  <p>Here's my form:</p>
  
  <pre><code>State:&lt;br /&gt;
  &lt;select name="state" id="stateName"&gt;
  &lt;?php foreach($rows as $row): ?&gt;
  &lt;option value="&lt;?php echo htmlentities($row['state'],ENT_QUOTES,'UTF-8');?&gt;"&gt;&lt;?php echo htmlentities($row['state'],ENT_QUOTES,'UTF-8');?&gt;
  &lt;/option&gt;
  &lt;?php endforeach; ?&gt;
  &lt;/select&gt;
  &lt;br /&gt;&lt;br /&gt;
  City:&lt;br /&gt;
  &lt;select name="city" id="cityName"&gt;&lt;/select&gt;
  &lt;input type="submit" name="work_order_submit" id="" value="Add Work Order" /&gt;
  </code></pre>
  
  <p>I populate the State dropdown with this query:</p>
  
  <pre><code>$query = "SELECT * FROM state WHERE 1 ORDER BY state";
  try{
  $stmt = $db-&gt;prepare($query);
  $stmt-&gt;execute();
  }catch(PDOException $ex){
  //On production remove getMessage.
  die("Failed to run query: " . $ex-&gt;getMessage());
  }
  
  $rows = $stmt-&gt;fetchAll();
  </code></pre>
  
  <p>Here's the jQuery I've created to run the JSON and populate the City dropdown with the cascaded values from the state dropdown when a state is selected:</p>
  
  <pre><code>&lt;script&gt;
  function populateCityName(){
  $.getJSON('state-names.php', {stateName:$('#stateName').val()},
      function(data){
              var select = $('#cityName');
              var options = select.prop('options');
              $('option', select).remove();
  
              $.each(data, function(index, array){
                       options[options.length] = new Option(array['city']);
                       });
  
            });
  }
  
  $(document).ready(function(){
    populateCityName();
    $('#stateName').on('change', function(){
          populateCityName();
    });
  });
  &lt;/script&gt;
  </code></pre>
  
  <p>And here's the code in the state-names.php file (I connect to the database before this code):</p>
  
  <pre><code>$rows = array();
  if(isset($_GET['stateName'])){
  $query = "SELECT city FROM city INNER JOIN state ON city.state_state_id = state.state_id WHERE state = :state ORDER BY city";
  $query_params = array(":state" =&gt; $_GET['stateName']);
  try{
      $stmt = $pdo-&gt;prepare($query);
      $stmt-&gt;execute($query_params);
  }catch(PDOException $ex){
      //On production remove .getMessage.
      die("Failed to run query: " . $ex-&gt;getMessage());
  }
  $rows = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC);
  }
  echo json_encode($rows);
  </code></pre>
  
  <p>Nothing happens when I select the state in the form. I don't even get an error. I've tested the SQL and it runs fine and retrieves the values that I want, but for some reason that I can't figure out, the values aren't being cascaded into the City dropdown.</p>
  
  <p>Any help is very much appreciated.</p>
  
  <p><strong>EDIT:</strong> As I'm doing more debugging and realizing a few things, this is what I've found so far. The JSON comes in like this:</p>
  
  <pre><code>[{"city":"Salt Lake City"},{"city":"Toole"},{"city":"Provo"},{"city":"St. George"}]
  </code></pre>
  
  <p>I now know the JSON is working correctly. When I changed the dropdown selection in the state dropdown to a state that I know had entries for the cities, the city dropdown showed blank 'option' fields for the number of entries that there actually were. So, using the answer below and MANY different tutorials on cascading dropdowns and chained dropdowns, I finally figured it out. I've submitted an answer with the working code. </p>