title: select data from database based on the data selected from dropdown
body: <p>I have a select element populated from database, when I select one element, I want to show me some other data from another column related to that row.</p>
  
  <p>For example: 
  on the dropdown I have:
  apples,
  bananas,
  pears</p>
  
  <p>When I select apple I want to select from database the column 'total' that shows me how many apples do I have left.</p>
  
  <p>I think, I should use Javascript-Ajax? Or is it another way to do this with php.</p>
  
  <p>Thanks in advance! </p>
  
  <pre><code>&lt;?php include("db.php"); ?&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;script type="text/javascript"&gt;
      $(document).ready(function() {
          $('select').material_select();
      }); 
  
      function myFunction() {
          if (document.getElementById('selectid').value == "other") {
              var x = document.createElement("INPUT");
              x.setAttribute("type", "text");
              x.setAttribute("value", "");
              x.setAttribute("placeholder", "Write another");
              x.setAttribute("class", "input-field col s12");
              document.getElementById("myDIV").appendChild(x);
          }
      } 
      &lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
      &lt;div id="myDIV" class="input-field col s4"&gt;
      &lt;?php
      $sql12 = "select * from fruitswhere total&gt;0 &amp;&amp; spent=1";
      $res1 = mysqli_query($conn, $sql12) or die("Error");
      $select = '&lt;select id="selectid" name="name" onchange="myFunction()"&gt;&lt;option value = "" disabled selected&gt; Choose &lt;/option&gt;';
      while($row1 = mysqli_fetch_assoc($res1)) {
          $emri = $row1['name'];
      // $select.='&lt;option value = "'.$row1['name'].'"&gt;'.$row1['name '].'&lt;/option&gt;';
          $select .= '&lt;option id="my_option" value="'.$name.'"&gt;'.$name.'&lt;/option&gt;';
      }
      $select .= '&lt;option value="other"&gt;other&lt;/option&gt;';
      $select .= '&lt;/select&gt;';
      echo $select;
      ?&gt;
      &lt;/div&gt;
  
      &lt;div class="input-field col s4"&gt;
      &lt;?php
      $query2 = "SELECT total FROM fruits WHERE name='$name'";
      $res2 = mysqli_query($conn, $query2);
      while($row2=mysqli_fetch_assoc($res2)){
          $all = $row2['total'];
      }
      ?&gt;
      &lt;input name="total" type="number" min="1"&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>