我试图做两个下拉框从数据库使用php和ajax填充。第一个下拉框只有一个选择,但是当您选择它时,它应该填充第二个下拉框,其中来自我的学校的不同部门从数据库中拉出。在选择部门时,应该在第三个下拉框中显示该部门的所有课程。现在我只是试图让所有部门的下拉框填充,当你点击它时,它不会填充任何东西。我认为循环在下拉框中写入部门名称存在问题。这里是我的代码级联下拉框拉从分区
的index.php
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script language="javascript" type="text/javascript">
function getXMLHTTP() { //function to return the xml http object
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}
return xmlhttp;
}
function getDept() {
var strURL="findDept.php";
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.
getElementById("deptdiv").
innerHTML=req.responseText;
} else {
alert("There was a problem " +
"while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
function getCourse(deptId) {
var strURL="findCourse.php?dept="+deptId;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.
getElementById('coursediv').
innerHTML=req.responseText;
} else {
alert("There was a problem " +
"while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
</script>
</head>
<body bgcolor= 'red' >
<p>
<font size="8" face="arial"
color="black">WELCOME TO SCHEDULE BUILDER</font>
</p>
<form method="post" action="" name="form1">
<table width="60%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width = "150">Semester</td>
<td width="150">
<select name="semester onChange="getDept()">
<option value="">Select Semester</option>
<option value ="1">Spring 2012</option>
</select>
</td>
</tr>
<tr style="">
<td>Department</td>
<td >
<div id="deptdiv">
<select name="department">
<option>Select Department</option>
</select>
</div>
</td>
</tr>
<tr style="">
<td>Course</td>
<td >
<div id="coursediv">
<select name="course">
<option>Select Department First</option>
</select>
</div>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</form>
</body>
</html>
findDept.php
<?
$link = mysql_connect("sql2.njit.edu", "sk442_proj", "jZ1MOA0X");
if (!link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("sk442_proj");
$query="SELECT abbrev FROM department";
$result=mysql_query($query);
?>
<select name="department" onchange="getCourse(this.value)">
<option>Select Department</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=$row['abbrev']><?=$row['abbrev']?></option>
<? } ?>
</select>
你有一个缺少的引号: