I'm getting an error in my 3 conditional drop down
A drop-down box (populated with MySQL data) and the 2nd drop down box data (populated with MySQL data) will be based on the 1st drop down box and the 3rd drop down box data (populated with MySQL data) will be based on the 2nd drop down box..
I have created the 2 dropdowns but when I tried the third dropdown I can't make it work. Here is my full source code
You can find the example of it here : 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开发者_如何学Python_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
>
That code is quite messy, no returns are checked. No prevention against sql injection.
At first glance I believe your line:
$cat3=$_GET['subcat'];
should be
$cat3=$_GET['cat3'];
精彩评论