我有一个这样的表格,如果我选择管理员级别,则禁用组合框部门,然后在选择人员级别时,组合框部门将处于活动状态并显示部门的选项,此方法有效,但在保存过程中通过选择管理员级别遇到了错误,并且表单部门未处于活动状态,
这是我的代码
<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);
}
}
});
});
代码保存程序
<?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
}
?>
代码搜索
<?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
}
?>
答案 0 :(得分:2)
如果该字段被禁用,您将不会发送$_POST['department'];
,因此:
$deparment = $_POST['department'];
无法加载您的帖子请求的'department'
索引。您可以这样做:
$department = isset($_POST['department']) ? $_POST['department'] : "Default value";
请注意,您的代码非常不安全,因为您不对值进行任何检查并且不对字符串进行转义,因此有人可以轻松地在表单内注入一些SQL。参见mysqli_real_escape_string()
。
我会至少转义您的字符串:
$name = mysqli_real_escape_string($_POST['name']);
$user = mysqli_real_escape_string($_POST['username']);
$password = md5($_POST['password']);
$level = mysqli_real_escape_string($_POST['level']);
$deparment = isset($_POST['department']) ? mysqli_real_escape_string($_POST['department']) : "Default value";
此外,正如@alexis观察到的那样,最好使用Prepared statements。
答案 1 :(得分:0)
基本上,该错误告诉您在第9行的“ add_user.php”文件中,您尝试打印实际上不存在的索引。 该行代码在下面给出...
<option value="<?php echo $data_prov["department"] ?>"><?php echo $data_prov["department"] ?></option><br
>
因此,为了解决此问题,您必须找出数据库表department
中的列名称是什么。只需在这些代码行中键入或复制完全相同的列名而不是department
即可粘贴到下面的代码行中...
<option value="<?php echo $data_prov["column_name"] ?>"><?php echo $data_prov["column_name"] ?></option><br`>
然后将解决此错误。