[Solved] Dynamic dropdown list


Easiest way of creating dynamic dropdown is by using jquery inside the head tag.
Give onchange() event and guide the data to another page using jquery code, and then link 2 dropdowns. The code I did is like this, which I felt is the easiest way for dynamic dropdowns.

jquery which I included is

<script>
  function ajaxDrp(data){
    $.ajax({
      method: "POST",
      url: "chapterDropdown.php",
      data: { 
        id: data
      }
    }).success(function(data) {
      $('#selectCourse').empty();
      $('#selectCourse').append(data);
    });
  }
</script>

#selectCourse is the id I have given to the other dropdown which have to be in sync with the first dropdown.

The given url in the jquery is the path where data of first dropdown is getting collected. In my case, the code is like this:

<?php

  $id = $_REQUEST['id'];
  $query = "SELECT * FROM `your table name` WHERE subject_id = " . $id;
  $result = mysqli_query($dbhandle,$query);
  $count = 0;
  $option`enter code here` = '';
  while($row = mysqli_fetch_assoc($result)) {
    $option .= '<option
    value="'.$row['id'].'">'.$row['course_name'].'</option>';
  }
  echo $option;
?>

solved Dynamic dropdown list