I am trying to fetch food name from its corresponding category name. I mean, i need when a user selects a category from first drop down, then from second drop down its related food names attached to that category should be displayed. For example, i selected Category Pizza, then in food names, food items that are allocated to Category Pizza should be displayed. But i am unable to fetch food names. Here is what i have done so far
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Select Category</label>
<div class="styled-select">
<select name="category">
<option>Select Category</option>
<?php
$SelectMainCats = mysqli_query($con, "SELECT * FROM categories WHERE `merchant_id` IN ('".$userId."','0')");
while($row=mysqli_fetch_assoc($SelectMainCats)){
?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['cat_name']; ?></option>
<?php } ?>
</select>
</div>
</div>
</div>
<script>
function showHint1(str) {
if (str.length == 0) {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
}
xmlhttp.open("GET", "get_food_items.php?cat_id="+str, true);
xmlhttp.send();
}
}
</script>
<span id="txtHint">
<div class="col-md-12 form-group">
<label for="">Select Food</label>
<select class="form-control" name="sub_cat_id">
<option value="">Select Food</option>
</select>
</div>
</span>
</div>
Then get_food_items.php code
<?php
error_reporting(0);
session_start();
include("include/config.php");
$cat_id = $_GET['cat_id'];
?>
<div class="col-md-12 form-group">
<label for="">Select Food</label>
<select class="form-control" name="sub_cat_id" required >
<option value="">Select Food</option>
<?php
$SelectmainCat = mysqli_query($con, "SELECT * FROM food WHERE category='$cat_id' ");
while($row=mysqli_fetch_assoc($SelectmainCat)){
?>
<option value="<?= $row['food_id']; ?>"><?= $row['food_name']; ?></option>
<?php } ?>
</select>
</div>
DB images are attached as well