I am quite new to PHP and JQuery and am struggling with 3 dynamic select boxes. The first one should contain a list of my instruments. The second one a list of categories, and the third should contain a list of subcategories, based on the selected value of the category and selected instrument. Have followed some great tutorials on the matter, but none seem exactly what I need. So far, managed to populate the instruments and the categories select box correctly, but when clicking on the categories select box to select the value I want, it malfunctions, the subcategories box stay empty. I believe the problem is because I do not send the instrumentID correctly when the categories onchange occurs, but cannot seem to find how to properly send it. Can anyone help me please ? This is my code so far :
<?php
    $query = "SELECT * FROM instruments ORDER BY name ASC";
    $result = $db->query($query)->results();
  ?>
<div class = "form-group col-md-3>"
  <select name="instrument_id" id="instrument_id">
    <option value="">-Select Instrument-</option>
    <?php
      foreach($result as $row){
        echo '<option value="'.$row->id.'">'.$row->name.'</option>';
      }
    ?>
</select>
</div>
<div class="form-group col-md-3">
      <label for="category_id" class="control-label">Category</label>
      <select id="category_id" name="category_id" class="form-control input-sm">
        <option value="">-Select Category-</option>
      </select>
</div>
<div class="form-group col-md-3">
  <label for="subcategory_id" class="control-label">Subcategory</label>
  <select id="subcategory_id" name="subcategory_id" class="form-control input-sm">
    <option value="">-Select Subcategory-</option>
  </select>
</div>
    <script>
$(document).ready(function(){
$('#instrument_id').on('change', function(){
    const instrumentID = $(this).val();
    if(instrumentID){
        $.ajax({
            type:'POST',
            url:'<?=PROOT?>admindocuments/get_categories',
            data:{instrument_id: instrumentID},
            success:function(html){
                $('#category_id').html(html);
                $('#subcategory_id').html('<option value="">-Select  Subcategory-</option>');
            }
        });
    }else{
            $('#category_id').html('<option value="">-Select Category-   </option>');
        $('#subcategory_id').html('<option value="">-Select Subcategory-  </option>');
    }
});
$('#category_id').on('change', function(){
    const categoryID = $(this).val();
    const instrumentID = $('#instrument_id').val();
    if(categoryID){
        $.ajax({
            type:'POST',
            url:'<?=PROOT?>admindocuments/get_subcategories',
            data: {
              category_id: categoryID,
              instrument_id: instrumentID,
            },
            success:function(html){
                $('#subcategory_id').html(html);
            }
        });
    }else{
        $('#subcategory_id').html('<option value="">-Select Subcategory-      </option>');
    }
  });
});
</script>
And this is the code in my get_categories.php and get_subcategories.php file :
get_categories :
<?php    
  if($_POST["instrument_id"]){
  $query = "SELECT * FROM categories ORDER BY name ASC";
    $result = $db->query($query)->results();
    echo '<option value="">-Select Category-</option>';
    foreach($result as $row){
    echo '<option value="'.$row->id.'">'.$row->name.'</option>';
  }
}
?>
get_subcategories :
<?php
    if($_POST["category_id"] && !empty($_POST["instrument_id"])){
  $query = "SELECT * FROM subcategories WHERE category_id =  ".$_POST['category_id']." AND instrument_id = ".$_POST['instrument_id']."  ORDER BY name ASC";
      $result = $db->query($query)->results();
    echo '<option value="">-Select Subcategory-</option>';
    foreach($result as $row){
      echo '<option value="'.$row->id.'">'.$row->name.'</option>';
    }
}
What am I doing wrong ? Please help me. Kind regards
 
    