I have a select "groupsId" on my page. When I select a value in it, I have a new select "category". It all works and I get the values. But when, when selecting a category, I try to get another select "subcategory", then it does not appear and the ajax request does not pass. Tell me what to do.
Here is my js
$('#groupId').change(function(e) {
//Grab the chosen value on first select list change
var selectval = $(this).val();
if (selectval == "") {
//Display initial prompt in target select if blank value selected
$('#CategorySelect').html("");
} else {
//Make AJAX request, using the selected value as the GET
$.ajax({url: './include/ajax/CategoryOption.php?cvalue='+selectval,
success: function(output) {
//alert(output);
$('#CategorySelect').html(output);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status + " "+ thrownError);
}});
}
});
$('#categoryId').change(function(e) {
//Grab the chosen value on first select list change
var selectvalue = $(this).val();
if (selectvalue == "") {
//Display initial prompt in target select if blank value selected
$('#subCategorySelect').html("");
} else {
//Make AJAX request, using the selected value as the GET
$.ajax({url: './include/ajax/SubCategoryOption.php?svalue='+selectvalue,
success: function(output) {
//alert(output);
$('#subCategorySelect').html(output);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status + " "+ thrownError);
}});
}
});
index.php
<form class="form-horizontal" method="POST" enctype="multipart/form-data"
<div class="form-group">
<label for="group" class="col-sm-2 control-label">Departments:</label>
<div class="col-sm-10">
<select class="form-control" id="groupId" name="groupId">
<?php department::DepartmentsList(); ?>
</select>
</div>
</div>
<div id="CategorySelect"></div>
<div id="subCategorySelect"></div>
</form>
CategoryOption.php
<?php
$mysqli = dbConnect();
$selectval = $_GET['cvalue'];
if (!is_numeric($selectval)){
echo "Invalid Data";
exit;
}
$sql = "select сt.name as categorynames,
g.name as groupname,
g.id as categoryid,
сt.id as groupid
from categories сt, groups g
where сt.groupid = g.id
and сt.groupid = $selectval";
$result = $mysqli->query($sql);
$mysqli->close();
if ($result->num_rows > 0) {
// output data of each row
echo '<div class="form-group">
<label for="category" class="col-sm-2 control-label">Категория</label>
<div class="col-sm-10">
<select class="form-control" id="categoryId" name="categoryId">
<option>Select category</option>';
while($row = $result->fetch_assoc()) {
echo '<option value="'.$row['categoryid'].'">' .$row['categorynames']. '</option>';
}
echo '</select>
</div>
</div>';
} else {
}
?>