【问题标题】:I'm getting an error in my 3 conditional drop down我的 3 条件下拉菜单中出现错误
【发布时间】:2011-02-16 03:23:57
【问题描述】:

一个下拉框(填充 MySQL 数据)和第二个下拉框数据(填充 MySQL 数据)将基于第一个下拉框和第三个下拉框数据(填充 MySQL 数据)将基于第二个下拉框..

我已经创建了 2 个下拉菜单,但是当我尝试第三个下拉菜单时,我无法让它工作。这是我的完整源代码

你可以在这里找到它的例子:http://www.plus2net.com/php_tutorial/dd3.php

  <?php

require "config.php"; // Your Database details 
?>

<!doctype html public "-//w3c//dtd html 3.2//en">

<html>

<head>

<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value; 
self.location='aw.php?cat=' + val ;
}
function reload3(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value; 
var val2=form.subcat.options[form.subcat.options.selectedIndex].value; 

self.location='aw.php?cat=' + val + '&cat3=' + val2 ;
}

</script>
</head>

<body>
<?

///////// Getting the data from Mysql table for first list box//////////
$quer2=mysql_query("SELECT DISTINCT StudNo,LName,FName,MName,Course FROM students"); 
///////////// End of query for first list box////////////

/////// for second drop down list we will check if category is selected else we will display all the subcategory///// 
$cat=$_GET['cat']; // This line is added to take care if your global variable is off
if(isset($cat) and strlen($cat) > 0){
$quer=mysql_query("SELECT DISTINCT GSCode,GStudNo,GSem,GYear,Grade FROM grade WHERE GStudNo='$cat' order by GSCode"); 
}else{$quer=mysql_query("SELECT DISTINCT GSCode,GStudNo,GSem,GYear,Grade FROM grade order by GSCode"); } 
////////// end of query for second subcategory drop down list box ///////////////////////////
$quer2=mysql_query("SELECT DISTINCT GSCode,GStudNo,GSem,GYear,Grade FROM grade"); 

/////// for Third drop down list we will check if sub category is selected else we will display all the subcategory3///// 
$cat3=$_GET['subcat']; // This line is added to take care if your global variable is off
if(isset($cat3) and strlen($cat3) > 0){
$quer3=mysql_query("SELECT DISTINCT GSem,GYear,Grade FROM grade where GSCode='$cat3'"); 
}else{$quer3=mysql_query("SELECT DISTINCT GSem,GYear,Grade FROM grade"); } 
////////// end of query for third subcategory drop down list box ///////////////////////////


echo "<form method=post name=f1 action='dd3ck.php'>";
//////////        Starting of first drop downlist /////////
echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
while($noticia2 = mysql_fetch_array($quer2)) { 
if($noticia2['StudNo']==@$cat){echo "<option selected value='$noticia2[StudNo]'>$noticia2[StudNo]</option>"."<BR>";}
else{echo  "<option value='$noticia2[StudNo]'>$noticia2[StudNo]</option>";}
}
echo "</select>";
//////////////////  This will end the first drop down list ///////////

//////////        Starting of second drop downlist /////////
echo "<select name='subcat' onchange=\"reload3(this.form)\"><option value=''>Select one</option>";
while($noticia = mysql_fetch_array($quer)) { 
if(empty($noticia['Grade']) AND $noticia['GSCode']==@$cat3){echo "<option selected value='$noticia[GSCode]'>$noticia[GSCode]</option>"."<BR>";}
else{echo  "<option value='$noticia[GSCode]'>$noticia[GSCode]</option>";}
}
echo "</select>";
//////////////////  This will end the second drop down list ///////////


//////////        Starting of third drop downlist /////////
echo "<select name='subcat3' ><option value=''>Select one</option>";
while($noticia = mysql_fetch_array($quer3)) { 
echo  "<option value='$noticia[GSem]'>$noticia[GSem]</option>";
}
echo "</select>";
//////////////////  This will end the third drop down list ///////////


echo "<input type=submit value='Submit the form data'></form>";
?>

</body>

</html

>

【问题讨论】:

    标签: php javascript mysql forms


    【解决方案1】:

    该代码非常混乱,没有检查任何返回。没有防止sql注入。

    乍一看我相信你的台词:

    $cat3=$_GET['subcat'];
    

    应该是

    $cat3=$_GET['cat3'];
    

    【讨论】:

    • 谢谢!但似乎空函数在第二次选择时不起作用..
    • 我的意思是它正在工作,但似乎我无法按照我想要的方式工作,就像它在我创建的 2 下拉菜单上的工作方式一样。它显示了所有记录,即使是那些不为空的记录..我使用的代码与我在 2 下拉菜单中使用的代码相同,但它的工作方式不同
    • 代码确实需要整理一下。我相信带有empty() 签入的 if 语句并没有按照您的意愿行事。
    • 我尝试了很多组合,但我仍然无法得到我想要发生的事情..我只想显示那些为空的记录..当我在 2 下拉列表中使用该代码时它可以工作但是在 3 下拉框中,它似乎更复杂.. 我想只显示空的记录,但是当它重新加载时它总是显示相同的项目,即使我选择其他项目..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    相关资源
    最近更新 更多