从第一个内容填充第二个下拉框

时间:2012-04-13 05:07:46

标签: php

我终于从我的数据库中填充了第一个下拉框,但现在第二个下拉框甚至没有出现,我觉得有些东西搞砸了我的if语句调用第二个函数,但不确定是什么也不确定我的我的查询中“WHERE”语句是正确的这是我的代码     

echo "<tr>";
echo "<td>Department</td>";
echo "<td><div id=\"deptdiv\"><select name=\"department\">";
echo "<option>Select Department</option>";
echo "</td>";
while($row=mysql_fetch_array($result))
{
echo '<option value="'.$row['abbrev'].'">'.$row['abbrev'].'</option>';
}

if($_GET[department] == "department" && isset($_GET[department])) {
drop_2($_GET[department]);
}
function drop_2($id)
{
$query = mysql_query("SELECT courseno, name FROM course WHERE department=$id");

echo "<tr>";
echo "<td>Course</td>";
echo "<td><div id \"coursediv\"><select name=\"course\">";
echo "<option>Select Course</option>";
echo "</td>";
while($r=mysql_fetch_array($query))
{
echo '<option value="'.$r['courseno'].'">'.$r['name'].'</option>';
}
}
?>

2 个答案:

答案 0 :(得分:0)

我对第一个下拉列表显示感到惊讶,因为您在<select>元素中有表标签,并且从未关闭该标记。我认为这是主要问题。试试:

echo "<td><div id=\"deptdiv\"><select name=\"department\">";
echo "<option>Select Department</option>";
while($row=mysql_fetch_array($result))
{
  echo '<option value="'.$row['abbrev'].'">'.$row['abbrev'].'</option>';
}
echo "</select></div></td>";

此外,您需要在GET变量周围加上引号,以便PHP不认为它们是$_GET['department']的常量,并关闭drop_2函数末尾的select标记。同样在函数中,在关闭select之后将</td>标记移动到。

希望有所帮助。

答案 1 :(得分:0)

这是您更正后的代码..

 echo "<tr>";
echo "<td>Department</td>";
echo "<td><div id=\"deptdiv\"><select name=\"department\">";
echo "<option>Select Department</option>";
while($row=mysql_fetch_array($result))
{
echo '<option value="'.$row['abbrev'].'">'.$row['abbrev'].'</option>';
}
echo "</div>";
echo "</td></tr>";
if($_GET['department'] == "department" && isset($_GET['department'])) {
drop_2($_GET['department']);
}
function drop_2($id)
{
$query = mysql_query("SELECT courseno, name FROM course WHERE department=$id");

echo "<tr>";
echo "<td>Course</td>";
echo "<td><div id \"coursediv\"><select name=\"course\">";
echo "<option>Select Course</option>";
while($r=mysql_fetch_array($query))
{
echo '<option value="'.$r['courseno'].'">'.$r['name'].'</option>';
}
echo "</div>";
echo "</td></tr>";
}
?>