Hi there,
I have been struggling with this problem for a while. The general idea is to have a few (3 +) listboxes on a one page form that change their depending on the previous list box selections. The tricky bit to this however is that the data for the listboxes has to come from a MySQL database. The code so far works for two listboxes with dynamic contents. But i need to expand this code for more than two listboxes please could somebody try to alter my code and show me where to insert what to get it to do more. Here it is
Thanks for your help
Alex S
Code so far:
<html>
<title>PHP/MySQL - Dynamic DropDowns</title>
<body>
<?
//Database Variables
$db_Database = "test";
$db_UserName = "root";
$db_Password = "";
$db_Hostname = "localhost";
//First Listbox Variables
$Firstboxtable = "facultys";
$Firstboxnames = "faculty";
$Firstboxid = "id";
//Second Listbox Variables
$Secondboxtable = "courses";
$Secondboxnames = "course";
$Secondboxid = "id";
$Secondboxref = "facultyid";
//Connect to the Database
mysql_connect($db_Hostname, $db_UserName, $db_Password) || UhOh("Can't Connect to Database: ".mysql_error());
mysql_select_db($db_Database);
echo "<form name=\"f1\" action='$PHP_SELF' method=\"post\">\n";
//read the database
$result = mysql_query("SELECT $Firstboxtable.$Firstboxnames, $Secondboxtable.$Secondboxref, $Secondboxtable.$Secondboxnames, $Secondboxtable.$Secondboxid FROM $Secondboxtable, $Firstboxtable WHERE $Secondboxtable.$Secondboxref=$Firstboxtable.$Firstboxid");
// write the faculty's listbox...
echo "Faculty<br><select name=\"faculty\" onchange=\"selected(this);\">\n";
// write the entry code for the javascript...\n is used to force a new line so the resultant code is more readable
$sJavaScript = "function selected(elem){\n for (var i = document.f1.$Secondboxnames.options.length; i >= 0; i--){ \n document.f1.$Secondboxnames.options[i] = null;\n";
// loop through the database..
$sLastfaculty="";
echo "<option value=\"#\" selected>Please Select</option>";
while ( $row = mysql_fetch_array($result))
{
// is this a new faculty?
If ($sLast!=$row["$Firstboxnames"]){
// if yes, add the entry to the facultys listbox
$sLast = $row["$Firstboxnames"];
echo "\n<option value='".$row["$Secondboxref"]."'>".$sLast."</option>";
// and add a new section to the javascript...
$sJavaScript = $sJavaScript."}\n"."if (elem.options[elem.selectedIndex].value==".$row["$Secondboxref"]."){\n";
}
// and add a new course line to the javascript
$sJavaScript = $sJavaScript."document.f1.$Secondboxnames.options[document.f1.$Secondboxnames.options.length] = new Option('".$row["$Secondboxnames"]."','".$row["$Secondboxid"]."');\n";
}
// finish the faculty's listbox
echo "</select><br><br>";
// create the course listbox for no selection
echo "Course<br><select name=\"course\" size=\"1\">";
echo "<option>[no $Secondboxnames selected]</option>";
echo "</select><br>";
echo "<input type=\"submit\" name=\"submit\" value=\"SUBMIT\">";
echo "</table>";
// finish the javascript and write out
$sJavaScript = $sJavaScript."\n}\n}\n";
echo "\n<SCRIPT LANGUAGE=\"JavaScript\">";
echo "\n".$sJavaScript."\n</SCRIPT>\n";
//close the form
echo "</FORM></center>";
if ("SUBMIT" == $submit){
echo "<font color=\"#000000\" face=\"verdana,arial,helvetica\" size=\"2\">Your Selected $Firstboxnames index= ".$faculty."</font><br>";
echo "<font color=\"#000000\" face=\"verdana,arial,helvetica\" size=\"2\">Your Selected $Secondboxnames index= ".$course."</font><br>";
}
?>
</body>
</html>