title: PHP Driven Select Functions
body: <p>I have a drop down that is enabled by default and shows options populated from the database. When an option is selected that is now blank it enables the drop down to the side if it.</p>
  
  <pre><code>echo '
  &lt;script&gt;
     function check(){
        if(document.getElementById("company").value!="")
            document.getElementById("stores").disabled=false;
        else
            document.getElementById("stores").disabled=true;
                                  }
   &lt;/script&gt;
  
   &lt;label class="form-control-label" for="input-last-name"&gt;Company&lt;/label&gt;
   &lt;select type="text" id="company" name="company" class="form-control form-control-alternative" onchange="check()"&gt;
      &lt;option&gt;&lt;/option&gt;';
  
      $sql = "SELECT * FROM companies WHERE CompanyID != '4'";
      $result = $con-&gt;query($sql);
  
      if ($result-&gt;num_rows &gt; 0) {
          // output data of each row
          while($row = $result-&gt;fetch_assoc()) {
              echo '&lt;option value='.$row['CompanyID'].'&gt;'.$row['CompanyName'].'&lt;/option&gt;';
          }
      }
  
      echo'
      &lt;/select&gt;
      &lt;label class="form-control-label" for="input-last-name"&gt;Store &lt;/label&gt;
      &lt;select id="stores" name="stores" class="form-control form-control-alternative" disabled&gt;
          &lt;option&gt;&lt;/option&gt;';
  
          $sql = "SELECT * FROM stores";
          $result = $con-&gt;query($sql);
  
          if ($result-&gt;num_rows &gt; 0) {
              // output data of each row
              while($row = $result-&gt;fetch_assoc()) {
                  echo '&lt;option value='.$row['storeid'].'&gt;'.$row['storename'].'&lt;/option&gt;';
              }
          }
          echo '
          &lt;/select&gt;';
  </code></pre>
  
  <p>As you can see selecting an item from the companies dropdown enables the stores dropdown to be enabled. However at the moment it shows all stores - not stores assigned to that company the SQL needs to be</p>
  
  <pre><code>SELECT * FROM store  WHERE StoreID = $SelectedCompanyID
  </code></pre>
  
  <p>and not </p>
  
  <pre><code>SELECT * FROM store
  </code></pre>
  
  <p>I cannot work out a way to populate a variable to complete the query and reload the drop down correctly with correct stores without reloading the page and loosing the rest of the inputs already completed in the form.</p>
  
  <p>Any help would be appreciated.</p>