title: Get data from Database Using Ajax and PHP and Return Result as Dropdown list
body: <p>I have the idea of what i wanted but need assistance on how to get it done.Below is the scenerio: I have a two dropdwon. The First dropdown is fetched from the DB, which works fine. At the change event of the first dropdown,the system should go to the Database, and fetch the result into the next dropdown. see what I have done so far for assistance:
  JQUERY SECTION</p>
  
  <pre><code>&lt;script type="text/javascript" src="includes/scripts/newJquery.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  $(document).ready(function() {
      $("#locate").change(function(){
         var selectedloc = $("#locate option:selected").val();
         $.ajax({type: "POST",url:"process-loc.php",data:{loca:selectedloc}}).done(function(data){
              var ans=jQuery.parse(data);
             //using php-mysql before
             var ps = ans.res;
             $("#subloc").html(ps);
  
         });
      });
      });
      &lt;/script&gt;
  </code></pre>
  
  <p>FrontEnd(HTML)</p>
  
  <pre><code> &lt;tr&gt;
      &lt;th&gt;Primary Location:&lt;/th&gt;
      &lt;?php 
     $result = mysqli_query($connection,"SELECT * FROM tab_location");?&gt;
      &lt;td&gt;
      &lt;select name="locate" class="form-control" id="locate"&gt;
      &lt;option&gt;Select Main Location&lt;/option&gt;
      &lt;?php while($rw = mysqli_fetch_array($result)){ ?&gt;
      &lt;option value="&lt;?php echo $rw['location_name'];?&gt;"&gt;&lt;?php echo $rw['location_name'];?&gt;&lt;/option&gt;
        &lt;?php };?&gt;
      &lt;/select&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
     &lt;tr&gt;
      &lt;th&gt;Sub Location:&lt;/th&gt;
      &lt;td id="subloc"&gt;&lt;/td&gt;
    &lt;/tr&gt;
  </code></pre>
  
  <p>Process-loc.php</p>
  
  <pre><code>if(isset($_POST["loca"])){
          include 'includes/session.php';
          include 'includes/db_connection.php';
          include 'includes/functions.php';
  
  
          $main = $_POST["loca"];
  
  
           $gets = "SELECT * FROM tab_fltlocation WHERE mainloc='".$main."'";
           $get = mysqli_query($connection,$gets);
           $gt = mysqli_fetch_array($get);
  
  
           //$nos= $gt['opsNo'];
  
  
            if(mysqli_num_rows($get)&gt;=0)
            {
            echo json_encode(array("res"=&gt;$gt));//or do a dropdown using &lt;select name='subloc'&gt;&lt;option value=$gt['loc']&gt;$gt['loc']&lt;/option&gt;&lt;/select&gt;
            }else{
            echo json_encode(array("res"=&gt;"0"));
            }
  
         }
           ?&gt;
  </code></pre>
  
  <p>This is what I wants to be displayed on the Front End page for the use:
  $gt['loc']
  How can I achieve this.</p>