Cek Kode dibawah ini :
Kode Kotak Daftar Pertama Akan seperti di bawah ini:
<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF']?>">
Drop1
<?php
$mysqli = new mysqli("localhost", "root", "", "lists");
$result = $mysqli->query("SELECT * FROM main_list GROUP BY id ORDER BY id");
$option = '';
while ($row = $result->fetch_assoc()) {
$option .= '<option value = "'.$row['value'].'">'.$row['value'].'</option>';
}
?>
<select id="main" name="main">
<option selected=selected>Choose</option>
<?php echo $option; ?>
</select>
<div id="sublist"></div>
<input type="submit" name="submit" value="Submit" />
</form>
Kode Jquery Akan seperti di bawah ini:
$('#main').change(function(){
$.ajax({
url : 'secondlist.php',
data :{mainlist_id : $(this).val()},
dataType:'html',
type:'POST',
success:function(data){
$('#sublist').html(data);
}
});
});
Kode di secondlist.php halaman :
$mysqli = new mysqli("localhost", "root", "", "lists");
$result1 = $mysqli->query("SELECT * FROM sub_list as s, main_list as m where s.category_id = m.id and s.category_id = $_POST['mainlist_id'] GROUP BY value ORDER BY id ");
$option1 = '';
while ($row = $result1->fetch_assoc()) {
$option1 .= '<option value = "'.$row['value'].'">'.$row['value'].'</option>';
}
$output = 'Drop2 ';
$output .= '<select name="sub" id="sub">';
$output .= '<option value=" " disabled="disabled" selected="selected">Choose one</option>';
$output .= $option1;
$output .= '</select> ';
echo $output;
exit;
?>
Beberapa perubahan mungkin diperlukan terkait dengan kebutuhan Anda, tetapi pasti akan berhasil untuk Anda.
Beri tahu saya jika Anda memiliki pertanyaan!
Terima kasih!