title: changing a textbox value when selected index from a combobox
body: <p>I have a combobox that has data fetched from the database. When an index is selected from the combobox, a textbox value then gets a value from the database depending on the index selected from the combobox. I have with the following codes.</p>
  
  <pre><code>&lt;form action = '' method = "POST"&gt;
    &lt;b&gt; School ID: &lt;/b&gt;&lt;br /&gt;
    &lt;select name = "school_id" required id = "schoold_id" onchange="get_data(this.value)"&gt;
    &lt;option value = "" selected&gt;&lt;/option&gt;
    &lt;?php 
    $sql = "SELECT DISTINCT school_id FROM school";
    $result = $db-&gt;query($sql);
    if ($result-&gt;num_rows &gt; 0){
          while($row = $result-&gt;fetch_assoc()){
                echo "&lt;option value = ".$row["school_id"]."&gt;".$row["school_id"]."&lt;/option&gt;";
          }
    }
    ?&gt;
    &lt;/select&gt;&lt;br /&gt;&lt;br /&gt;
    &lt;b&gt; Batch Number: &lt;/b&gt;&lt;br /&gt;
    &lt;input type= "text" name = "batch_no" /required&gt;&lt;br /&gt;&lt;br /&gt;
    &lt;b&gt; School Name: &lt;/b&gt;&lt;br /&gt;
    &lt;input type= "text" name = "school_name" /readonly&gt;&lt;br /&gt;&lt;br /&gt;
    &lt;input type = "submit" value = " Submit "/&gt;&lt;br /&gt;
    &lt;/form&gt;
  </code></pre>
  
  <p>The combobox here is the school id and the textbox that needs to be changed is the school name.</p>
  
  <p>I tried using javascript and here it is that I used.</p>
  
  <pre><code>&lt;script&gt;
  function get_data(value){
  $.ajax({
      url: "ajax.php",
      type: "POST",
      dataType: "HTML",
      async: false,
      data: {value: value}
      success: function(data) {
         //here we can populate the required fields based on value from database                
      }
   });
  }&lt;/script&gt;
  </code></pre>
  
  <p>and this is the ajax file</p>
  
  <pre><code>&lt;?php
  include "connect.php";
  $myschoolid = (filter_input(INPUT_POST, 'school_id'));
  
  $sql = $db-&gt;prepare('SELECT school_name FROM main where school_id = ?');
  $sql-&gt;bind_param('i', $myschoolid);
  $sql-&gt;execute();
  $result = $sql-&gt;get_result();
  if ($result-&gt;numrows &gt;0){
      while ($row = $result-&gt;fetch_assoc()){
          $newschoolid = $row["school_name"];
      }
      echo $newschoolid;
  }
  ?&gt;
  </code></pre>
  
  <p>I'm probably doing something wrong here. I'm not entirely familiar with ajax. Would appreciate help.</p>