A dependent dropdown loads the second list after the user selects a value in the first list. Reset stale child options before requesting the new data.
<select id="category"></select>
<select id="subcategory" disabled></select>
$( "#category" ).on(
"change",
function () {
const categoryId =
$( this ).val();
const $child =
$( "#subcategory" )
.empty()
.prop(
"disabled",
true
);
if ( !categoryId ) {
return;
}
$.getJSON(
"subcategory-data.php",
{ category_id: categoryId }
)
.done(function ( rows ) {
$.each(
rows,
function ( index, row ) {
$( "<option>", {
value: row.id,
text: row.name
}).appendTo( $child );
}
);
$child.prop(
"disabled",
false
);
});
}
);
$categoryId = filter_input(
INPUT_GET,
'category_id',
FILTER_VALIDATE_INT
);
$stmt = $dbo->prepare(
'SELECT id, name
FROM subcategory
WHERE category_id = :category_id
ORDER BY name'
);
$stmt->execute([
'category_id' => $categoryId
]);
Demo: two dependent dropdowns →
Show record details after the second selection →
<form method=post action=dd-submit.php>
<select name=category id=category>
<option value='' selected>Select</option>
<?Php
require "config.php";// connection to database
$sql="select * from dd2_category "; // Query to collect data
foreach ($dbo->query($sql) as $row) {
echo "<option value=$row[cat_id]>$row[category]</option>";
}
?>
</select>
<select name=sub-category id=sub-category>
</select>
<input type=submit value=Submit></form>$('#category').change(function(){var cat_id=$('#category').val();$('#sub-category').empty();$.get('ddck.php',{'cat_id':cat_id},function(return_data){$.each(return_data.data, function(key,value){
$("#sub-category").append("
<option value=" + value.subcat_id +">"+value.subcategory+"</option>
");
});
}, "json");<?Php
@$cat_id=$_GET['cat_id'];
//$cat_id=2;
/// Preventing injection attack ////
if(!is_numeric($cat_id)){
echo "Data Error";
exit;
}
/// end of checking injection attack ////
require "config.php";
$sql="select dd2_subcategory,subcat_id from subcategory where cat_id='$cat_id'";
$row=$dbo->prepare($sql);
$row->execute();
$result=$row->fetchAll(PDO::FETCH_ASSOC);
$main = array('data'=>$result);
echo json_encode($main);
?>$result = array();
if($stmt = $connection->prepare("select subcategory,subcat_id from dd2_subcategory where cat_id=?")){
$stmt->bind_param('i',$cat_id);
$stmt->execute();
$stmt = $stmt->get_result();
$no_of_records=$stmt->num_rows;
while ($row = $stmt->fetch_assoc()) {
$result[]=$row;
}
}else{
echo $connection->error;
}
$main = array('data'=>$result,'no_of_records'=>$no_of_records);
echo json_encode($main);<?Php
$category=$_POST['category'];
$sub_category=$_POST['sub-category'];
echo "Category : $category <br>
Sub-category = $sub_category ";
?>$no_of_records = $dbo->query("select count(cat_id) from dd2_subcategory where cat_id='$cat_id'")->fetchColumn();
if($no_of_records >=1){
$sql="select subcategory,subcat_id from dd2_subcategory where cat_id='$cat_id'";
$row=$dbo->prepare($sql);
$row->execute();
$result=$row->fetchAll(PDO::FETCH_ASSOC);
}else{
$result='';
}
$main = array('data'=>$result,'no_of_records'=>$no_of_records);
echo json_encode($main);if(return_data.no_of_records>=1){
$('#t1').hide();
$('#sub-category').show();
$.each(return_data.data, function(key,value){
$("#sub-category").append("<option value='" + value.subcat_id +"'>"+value.subcategory+"</option>");
});
}else{
/// add text box and hide 2nd subcategory
$('#sub-category').hide();
$('#t1').show();
}
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.