您好我正在开发一个从数据库中提取记录的php下拉列表。我有第一次和第二次下降工作。但是我需要帮助实现第3次下拉。这是我到目前为止所拥有的。前2个工作正常。我真的需要让第三个工作。它依赖于第一个和第二个的输入。
的index.php
<?php
require_once("dbcontroller.php");
$query ="SELECT * FROM campus";
?>
<html>
<head>
<TITLE>Campus and Faculty Select</TITLE>
<head>
<style>
body{width:610px;}
.frmDronpDown {border: 1px solid #F0F0F0;background-color:#C8EEFD;margin: 2px 0px;padding:40px;}
.demoInputBox {padding: 10px;border: #F0F0F0 1px solid;border-radius: 4px;background-color: #FFF;width: 50%;}
.row{padding-bottom:15px;}
</style>
<script src="jquery.min.js" type="text/javascript"></script>
<script>
function getcampus_id(val) {
$.ajax({
type: "POST",
url: "get_faculty.php",
data:'campus_id='+val,
success: function(data){
$("#faculty-list").html(data);
}
});
}
function getfaculty_id(val) {
alert(val)
$.ajax({
type: "POST",
url: "get_course.php",
data:'faculty_id='+val,
success: function(data){
$("#course-list").html(data);
}
});
}
function selectcampus_id(val) {
$("#search-box").val(val);
$("#suggesstion-box").hide();
}
</script>
</head>
<body>
<form id="rightcol" action="form.php" method="post" class="container">
<div class="frmDronpDown">
<div class="row">
<label>Campus:</label><br/>
<select name="campus" id="campus-list" class="demoInputBox" onChange="getcampus_id(this.value);">
<option value="">Select Campus</option>
<?php
$query ="SELECT * FROM campus";
$result = mysqli_query($con, $query);
//loop
while($campus = mysqli_fetch_assoc($result)) :
//foreach ($result as $campus){
?>
<option value="<?php echo $campus["campus_id"]; ?>"> <?php echo $campus["name"]; ?></option>
<?php
endwhile;
?>
</select>
</div>
<div class="row">
<label>Faculty:</label><br/>
<select name="faculty" id="faculty-list" class="demoInputBox" onChange="getfaculty_id(this.value);>
<option value="">Select Faculty</option>
</select>
</div>
<div class="row">
<label>Course:</label><br/>
<select name="course" id="course-list" class="demoInputBox" >
<option value="">Select Course</option>
</select>
</div>
</div>
</body>
</html>
get_faculty.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once("dbcontroller.php");
if(!empty($_POST["campus_id"])) {
$campus_id = $_POST["campus_id"];
$query ="SELECT * FROM faculty WHERE campus_id = $campus_id";
$result = mysqli_query($con, $query);
if ( $result === false ) {
echo mysqli_error($con);
exit;
}
echo '<option value="">Select Campus</option>';
while ( $faculty = mysqli_fetch_assoc($result) ) :
echo '<option value="' . $faculty['faculty_id'] . '">';
echo $faculty['faculty_name'];
echo '</option>';
endwhile;
}
?>
最后是get_course.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once("dbcontroller.php");
if(!empty($_POST["campus_id"])) {
$course_id = $_POST["campus_id"].$POST_["faculty_id"];
$query ="SELECT * FROM `course` WHERE `campus_id`and `faculty_id` = $course_id";
$result = mysqli_query($con, $query);
if ( $result === false ) {
echo mysqli_error($con);
exit;
}
echo '<option value="">Select Campus</option>';
while ( $course = mysqli_fetch_assoc($result) ) :
echo '<option value="' . $course['course_id'] . '">';
echo $faculty['course_name'];
echo '</option>';
endwhile;
}
?>
答案 0 :(得分:0)
您错过了第二个onchange
的{{1}}的结束语。
使用此
select
的修改
我试图复制你想要做的事情并让它发挥作用。以下是包含我在此gist
<select name="faculty" id="faculty-list" class="demoInputBox" onChange="getfaculty_id(this.value);">
<option value="">Select Faculty</option>
</select>
编辑的文件
我编辑的文件是
的index.php
在post请求中包含mysqldump
以获取course_list,以便我们可以将其用于外键
campus_id
get_course.php
更正您访问function getfaculty_id(val) {
$.ajax({
type: "POST",
url: "get_course.php",
data:{
'faculty_id':val,
'campus_id':$('#campus-list').val()
},
success: function(data){
$("#course-list").html(data);
}
});
}
数据的方式,不应该$_POST
正确的$POST_["faculty_id"]
正如@sean建议的那样
在while循环中使用$query
代替$course
来创建$faculty
select