I have a form like this, if I choose the Admin level, then the combo box department is disabled, then when choosing the Staff level, then the combo box department will be active and display the department's options, this method works, but it encountered an error during the save process by selecting the Admin level, and the form department is not active,
This my code
<div class="form-group">
<label class="control-label col-sm-4" for="level">Level</label>
<div class="col-sm-4">
<select name="level" id="level" class="form-control">
<option value="null">-- Level --</option>
<option value="admin">Admin</option>
<option value="staff">Staff</option>
<option value="client">Client</option>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4" for="department">Department</label>
<div class="col-sm-4">
<select name="department" id="department" class="form-control">
</select>
</div>
</div>
$("#level").change(function(){
// variabel dari nilai combo box provinsi
var id = $("#level").val();
// mengirim dan mengambil data
$.ajax({
type: "POST",
dataType: "html",
url: "search_level.php",
data: "level="+id,
success: function(msg){
// jika tidak ada data
if(msg == ''){
var x=document.getElementById("department")
x.value=""
x.disabled=true
}
// jika dapat mengambil data,, tampilkan di combo box kota
else{
var x=document.getElementById("department")
x.disabled=false
$("#department").html(msg);
}
}
});
});
code save proses
<?php
include "../config/koneksi.php";
$name = $_POST['name'];
$user = $_POST['username'];
$password = md5($_POST['password']);
$level = $_POST['level'];
$deparment = $_POST['department'];
$status = '1';
$tgl_dibuat = date("Y-m-d h:i:s");
$query = mysqli_query($con, "INSERT INTO user (nama,user,password,level,department,tgl_dibuat,status) VALUES ('$name','$user',
'$password','$level','$deparment','$tgl_dibuat','$status')");
if ($query) {
?>
<script language="JavaScript">
alert('User Saved');
</script>
<?php
} else {
?>
<script language="JavaScript">
alert('Failed');
document.location='user';
</script>
<?php
}
?>
code search
<?php
include "../config/koneksi.php";
$id = $_POST['level'];
$query = mysqli_query($con, "SELECT * FROM department WHERE level='$id'");
while($data_prov=mysqli_fetch_array($query)){
?>
<option value="<?php echo $data_prov["department"] ?>"><?php echo $data_prov["department"] ?></option><br>
<?php
}
?>