title: MySQL Query with PHP and Send Data to JavaScript
body: <p><strong>EDIT:</strong></p>
  
  <p>I have a number input with an ID on it. In a MySQL database, I have a table with all the IDs and a list of options separated by commas for that ID.</p>
  
  <p>I'll take one of the database records for example: The id is "ham_and_cheese" and the options are "Mayonnaise, Salad Cream, Chutney, No Dressing". Say the person selects 1 on the number input with the ID "ham_and_cheese". </p>
  
  <p>Then, 1 drop down will appear with the options: "Mayonnaise, Salad Cream, Chutney, No Dressing" (Each their own option). If they choose 2, then 2 dropdowns appear, 3 then 3 appear, etc.</p>
  
  <p>I am very new to Ajax so it would be great if someone could help me out.</p>
  
  <p>My new code:</p>
  
  <pre><code>...
  
  &lt;td&gt;&lt;input type='number' min='0' max='5' name='ham_and_cheese_amount' /&gt;&lt;/td&gt;
  
  ...
  
  &lt;td id='ham_and_cheese_dressing'&gt;&lt;/td&gt;
  
  ...
  
  function changeDropdown(name, amount){
  
      //Gets the id.
      var newName = name.replace("_amount", "");
  
      //Gets the div that the drop down will go in.
      var el2 = document.getElementById(newName + "_dressing");
  
      //If number 0 is selected in number input.
      if(amount == 0){
          el2.innerHTML = "";
      }
  
      //If number 1 is selected in number input.
      if(amount == 1){
          var html = "&lt;select&gt;";
          var id = newName;
          $.ajax({
              url: 'updateDropdown.php',
              dataType: 'json',
              data: {'option_id':id},
              type: 'get',
              success: function(r){
                  for(i = 0; i &lt; r.length; i++){
                      // I use r[i].toLowerCase().replace(" ", "_") to convert "Salad Cream" to the id "salad_cream", etc.
                      html += "&lt;option value='" + r[i].toLowerCase().replace(" ", "_") + "'&gt;" + r[i] + "&lt;/option&gt;";
                  }
                  html += "&lt;/select&gt;";
                  el2.innerHTML = html;
              }
          });
      }
  
  }
  </code></pre>
  
  <p>My updateDropdown.php:</p>
  
  <pre><code>&lt;?php
  
      include "/home/pi/config.php";
      $id = $_GET['option_id'];
      //I know that the connect works because I use the same code for other queries.
      $connect = mysqli_connect("localhost", $name, $pass, "items");
      if(mysqli_connect_errno()){
          return "Failed to connect to products database!";
      }
      $result = mysqli_query($connect, "SELECT options FROM products WHERE id='$id' LIMIT 1");
      $resultArray = explode(", ", mysqli_fetch_row($result)[0]);
  
      return json_encode($resultArray);
  
  ?&gt;
  </code></pre>