我的动态选择表单有问题。提交“主题”表单时效果很好,但是当提交“课程”表单时,表单似乎正确提交(localhost / page.php?subject = 1& course = 2),但它返回选项回到null
所以它不适用于以后依赖于course_id
的php。
标题PHP:
<?php
$subject = $course = null;
$conn = mysql_connect('', '', '');
$db = mysql_select_db('',$conn);
if(isset($_GET["subject"]) && is_numeric($_GET["subject"]))
{
$subject = $_GET["subject"];
}
if(isset($_GET["course"]) && is_numeric($_GET["course"]))
{
$country = $_GET["course"];
}
?>
使用Javascript:
<script language="JavaScript">
function autoSubmit()
{
var formObject = document.forms['theForm'];
formObject.submit();
}
</script>
HTML表单:
<form name="theForm" method="get">
<select name="subject" onChange="autoSubmit();">
<option value="null">Select a Subject...</option>
<?php
$sql = "SELECT DISTINCT subj_name, subj_id FROM table1 ORDER BY subj_name";
$result = mysql_query($sql) or die ("couldn't execute query");
while($row = mysql_fetch_array($result))
{
echo ("<option value=\"$row[subj_id]\" " .
($subject == $row["subj_id"] ? " selected" : "") . ">$row[subj_name]</option>");
}
?>
</select>
<?php
if($subject != null && is_numeric($subject))
{
?>
<select name="course" onChange="autoSubmit();">
<option value="null">Select a Course...</option>
<?php
$sql = "SELECT DISTINCT course_id, course_name, subj_id FROM table1 WHERE subj_id = $subject";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo ("<option value=\"$row[course_id]\" " .
($course == $row["course_id"] ? " selected" : "") . ">$row[course_name]</option>");
}
?>
</select>
<?php
}
?>
</form>
答案 0 :(得分:0)
在这些行中,只有在回调请求中未传递$ subject和$ course时才应设置“selected”:
<option value="null" selected="selected">Select a Subject...</option>
...
<option value="null" selected="selected">Select a Course...</option>
否则,您将在每个下拉菜单中获得两个标记为已选中的选项。这导致了错误。
答案 1 :(得分:0)
我发现错误:第14行中的$country
需要$course
:
if(isset($_GET["course"]) && is_numeric($_GET["course"]))
{
$course = $_GET["course"];
}
?>
答案 2 :(得分:0)
<pre>
if(isset($_GET["course"]) && is_numeric($_GET["course"]))
{
$country = $_GET["course"];
}
</pre>
Here, you are setting a $country variable instead of $course.
**Use this one**
$course= $_GET["course"];