I have groups and Categories. Categories are dependant of groups. When I select group, i should see related categories.
Everything works. My ajax request pass the correct group ID, the query than finds all the related categories of that passed group ID.
In network tab and console log, I can see the correct data.
Problem is .html(). After the success in Ajax request, I would like to populate in dropdown and its not working.
Here is mu Ajax
<script>
function getCategories(val) {
$.ajax({
type: "POST",
url: "../data/stock.php?action=select-related-categories",
data: 'group_id=' + val,
success: function (data) {
console.log(data);
$("#category").html(data);
}
});
}
</script>
The Dropdown of GROUP
<div class="col-md-6">
<select class="form-control group-select" id="group-select" name="rootid" onChange="getCategories(this.value);">
<?php while ($rowval = mysqli_fetch_array($group, MYSQLI_ASSOC)) { ?>
<option value="<?php echo $rowval['id_stc']; ?>">
<?php echo $rowval['description_stc']; ?>
</option>
<?php
}
?>
</select>
</div>
The Categories
<div class="col-md-6">
<select class="form-control" id="category" name="categoryid" onChange="getSets(this.value);">
</select>
</div>
The Query which looks for related categories
<?php
if (!empty($_POST["category_id"])) {
$query = "SELECT * FROM stockcategories_stc WHERE idstc_stc = '" . $_POST["category_id"] . "' Order by lastupdated_stc DESC ";
$result = mysqli_query($mysqli_scs, $query);
foreach ($result as $row) {
?>
<option value="<?php echo $row[" id_stc "]; ?>">
<?php echo $row["description_stc"]; ?>
</option>
<?php
}
}
?>
The ajax works fine. The only part not working is .html(data)
does not show any results in category dropdown.
Thank you