title: Using php/javascript to make 3 dependent drop down menus that are populated from a MySQL database
body: <p>Using php/javascript to make 3 dependent drop down menus that are populated from a MySQL database</p>
  
  <p>Hi! I am new to coding/web development. I know this is a popular topic and I have already spent hours reading forums and looking at sample code, however:</p>
  
  <p>a) Examples I have found include data that is hard-coded and does not pull the data from a database, which is what I need to do.
  b) The examples also use extentions like mysql_query and mysql_fetch_assoc which are now deprecated. I think I need to use PDO but I am unfamiliar with that. </p>
  
  <p>Here is some background about my database:</p>
  
  <p>There is only one table in the database--"costs_table." The fields that I am trying to populate the  drop downs with are "Subsystem," ,"Unit_Cost_element" and "Unit_Cost_component." There is a 1:M relationship between these fields- a subsystem has many elements, and an element has many components. </p>
  
  <p>Here is the SQL statements for what I want the code to be doing for each drop down: </p>
  
  <pre><code>SELECT DISTINCT Subsytem FROM Costs_table;
  
  SELECT DISTINCT Unit_Cost_Element FROM Costs_Table WHERE Subsystem = $SelectedSubsystem;
  
  SELECT DISTINCT Unit_Cost_Component FROM Costs_Table WHERE Subsystem = $SelectedSubsystem AND Unit_Cost_Element = $SelectedElement;
  </code></pre>
  
  <p>I know I will eventually need arrays but I have never constructed an array before. The code I have so far is just the index page that creates the list boxes. Also I got this code from another forum so it may not be 100% in-line with what I need to do. </p>
  
  <p>Anyways here is the code:</p>
  
  <pre><code>&lt;?php
  
  try {
      $objDb = new PDO('mysql:host=localhost;dbname=unit_costs_db', 'username', 'password');
      $objDb-&gt;exec('SET CHARACTER SET utf8');
  
      $sql = "SELECT DISTINCT Subsystem, ID FROM costs_table ORDER BY Subsystem ASC";
      $statement = $objDb-&gt;query($sql);
      $list = $statement-&gt;fetchAll(PDO::FETCH_ASSOC);
  
  }
      catch(PDOException $e){
      echo 'There was a problem';
          }
      ?&gt;
  
  &lt;!DOCTYPE HTML&gt;
  
  &lt;html lang="en"
  &lt;head&gt;
  &lt;meta charset="utf-8" /&gt;
  &lt;title&gt; Dependable Dropdown menu&lt;/title&gt;
  &lt;meta name="description" content="Dependable dropdown menu" /&gt;
  &lt;meta name="keywords" content="dependable dropdown menu" /&gt;
  &lt;link href="/css/6-19_core.css" rel="stylesheet" type="text/css" /&gt;
  
  &lt;/head&gt;
  
  
  &lt;body&gt;
  
  
  &lt;div id="wrapper"&gt;
      &lt;form action="" method="post"&gt;
  
              &lt;select name="Subsystem" id="Subsystem" class="update"&gt;
                  &lt;option value=""&gt;Select Subsystem&lt;/option&gt;
                  &lt;?php if (!empty($list)) { 
                      foreach($list as $row) { ?&gt;
                      &lt;option value="&lt;?php echo $row['ID']; ?&gt;"&gt;
                          &lt;?php echo $row['Subsystem']; ?&gt;
                      &lt;/option&gt;
                  &lt;?php }
                  }
                  ?&gt;
                  &lt;/select&gt;
  
              &lt;/select&gt;
                  &lt;br&gt;
              &lt;select name="Element" id="Element" class="update"
                  disabled="disabled"&gt;
                  &lt;option value=""&gt;----&lt;/option&gt;
              &lt;/select&gt;
                  &lt;br&gt;
              &lt;select name="Component" id="Component" class="update"
                  disabled="disabled"&gt;
                  &lt;option value=""&gt;----&lt;/option&gt;
              &lt;/select&gt;
  
      &lt;/form&gt;
  &lt;/div&gt;
  
  
  
  
  &lt;script src="/js/jquery-1.6.4.min.js" type="text/javascript"&gt;&lt;/script&gt;
  
  &lt;script src="/js/6-19_core.js" type="text/javascript"&gt;&lt;/script&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Any help with what I need to add to make this work properly would be greatly appreciated!!! Thanks in advance :)</p>