Jump to content

Dynamic Drop-Down Menu Question


surf5502

Recommended Posts

So I need to create 3 drop downs. each one feeds off the one above it. so the first dropdown is for a department, when you select that I need every class in that department to come up in the next dropdown. when you select a class i need ever professor for that class to appear in the third dropdown. so i had it working for a solid 10 min... now it doesnt work... (ive been programming php forever, but i am an intermediate with ajax so i feel the error somewhere in the javascript?)

 

HTML

<script type="text/javascript" src="../../js/upload.js"></script>
<form action="" method="post" enctype="multipart/form-data">
<?php if (($_POST['title']!="")&&(!validateForm())) echo "<center><b>Form Invalid</b></center>"; ?>
<?php if (isset($error)) echo "<center><b>Error during file upload</b></center>"; ?>
    <table width="550" align="center" cellpadding="0" cellspacing="0">
        <tr><td><b>Department:</b></td><td><select name="dept" onchange="showClass(this.value)">
            <option value="">-- Select Deptartment --</option>
            <?php
                $query = "SELECT * FROM `categ_dept` ORDER BY `name` ASC";
                $result = mysql_query($query);
                $rows = mysql_num_rows($result);
                $i = 0;
                while ($i < $rows){
                    $name = mysql_result($result, $i, "name");
                    $id = mysql_result($result, $i, "id");
                    echo "<option value=\"$id|$name\"";
                    if ($_POST['dept'] == $id)
                        echo " selected";
                    echo ">$name</option>
            ";
                    $i++;
                }
            ?>
        </select></td></tr>
        <tr><td><b>Class:</b><br/><br/></td><td><div id="txtClass"><select name="class" onchange="showProf(this.value)">
        <option value="">-- Select Deptartment First --</option>
        </select></div><br/><br/></td></tr>
        <tr><td colspan="2" style="border:1px solid #CCCCCC; border-bottom:none; text-align:center; padding-top:10px;"><b><i>If your class is not listed please use this box</i></b></td></tr>
        <tr><td style="border-left:1px solid #CCCCCC; padding:10px;"><b>Class Name:</b></td><td style="border-right:1px solid #CCCCCC;"><input type="text" name="class_name" value="<?php echo $_POST['class_name']; ?>" /></td></tr>
        <tr><td style="border-left:1px solid #CCCCCC; padding:10px;"><b>Call Number:</b><br/><i>ex: W3256</i></td><td style="border-right:1px solid #CCCCCC;"><input type="text" name="class_call" value="<?php echo $_POST['class_call']; ?>" /></td></tr>
        <tr><td style="border-left:1px solid #CCCCCC; padding:10px;"><b>Keywords:</b><br/><i>Seperate by spaces</i></td><td style="border-right:1px solid #CCCCCC;" valign="middle"><input type="text" name="class_keywords" value="<?php echo $_POST['class_keywords']; ?>" /></td></tr>
        <tr><td style="border:1px solid #CCCCCC; border-top:none; border-right:none; padding:10px;"><b>Description:</b></td><td style="border:1px solid #CCCCCC; border-top:none; border-left:none;"><textarea name="class_des" rows="4"><?php echo $_POST['class_des']; ?></textarea></td></tr>
        <tr><td><br/><b>Professor:</b><br/><br/></td><td><br/><div id="txtProf"><select name="professor">
        <option value="">-- Select Class First --</option>
        </select></div><br/><br/></td></tr>
        <tr><td colspan="2" style="border:1px solid #CCCCCC; border-bottom:none; text-align:center; padding-top:10px;"><b><i>If your professor is not listed please use this box</i></b></td></tr>
        <tr><td style="border:1px solid #CCCCCC; border-top:none; border-right:none; padding:10px;"><b>Professor Name:</b></td><td style="border:1px solid #CCCCCC; border-top:none; border-left:none;"><input type="text" name="prof_name" value="<?php echo $_POST['prof_name']; ?>" /></td></tr>
        <tr><td colspan="2" align="center" style="padding-top:30px;"><input type="submit" value="Upload" /></td></tr>
    </table>
    </form>

 

JAVASCRIPT - upload.js

var xmlHttp;
var showID = 0;
function showClass(str){ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
	alert ("Browser does not support HTTP Request");
	return;
}
showID = 1;
var url="../../includes/getDropdown.php";
url=url+"?q="+str;
url=url+"&sid="+showID;
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
function showProf(str){ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
	alert ("Browser does not support HTTP Request");
	return;
}
showID = 2;
var url="../../getDropdown.php";
url=url+"?q="+str;
url=url+"&sid="+showID;
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
function stateChanged(){
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
	if (showID==1){
		document.getElementById("txtClass").innerHTML=xmlHttp.responseText;
	}else{
		document.getElementById("txtProf").innerHTML=xmlHttp.responseText;
	}
} 
}
function GetXmlHttpObject(){
var xmlHttp=null;
try{
	// Firefox, Opera 8.0+, Safari
	xmlHttp=new XMLHttpRequest();
	if (http_request.overrideMimeType) {
		http_request.overrideMimeType('text/html');
	}
}catch (e){
	//Internet Explorer
	try{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	}catch (e){
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
}
return xmlHttp;
}

 

PHP - getDropdown.php

<?php
@session_start();
$q = $_GET['q'];
$sid = $_GET['sid'];
include ("config.php");
mysql_connect($hostaddr,$username,$password);
@mysql_select_db($database) or die("Error: Database not available. Please try again later.");
if ($sid == 1){
// q is a department id
// return classes for that department
echo "<select name=\"class\" onchange=\"showProf(this.value)\">
	";
$dept = explode("|",$q);
$query = "SELECT * FROM `categ_class` WHERE `dept_id`='".$dept[0]."' AND `school_id`='".$_SESSION['school']."' ORDER BY `name` ASC";
$result = mysql_query($query);
$rows = mysql_num_rows($result);
$i = 0;
if ($rows == 0){
	echo "<option value=\"\">No Classes Listed</option>";
}else{
	echo "<option value=\"\">-- Select a Class --</option>
        ";
	while ($i < $rows){
		$name = mysql_result($result, $i, "name");
		$call = mysql_result($result,$i,"call_num");
		$id = mysql_result($result, $i, "id");
		echo "<option value=\"$id\"";
		if ($_POST['class'] == $id)
			echo " selected";
		echo ">$name ($call)</option>
		";
		$i++;
	}
}
echo "</select>";
}else{
// q is a class id
// return professors for that class
echo "<select name=\"professor\">
	";
$query = "SELECT * FROM `categ_prof` WHERE `class_id`='".mysql_real_escape_string($q)."' ORDER BY `name` ASC";
$result = mysql_query($query);
$rows = mysql_num_rows($result);
$i = 0;
if ($rows == 0){
	echo "<option value=\"\">No Professors Listed</option>";
}else{
	echo "<option value=\"\">-- Select a Professor --</option>
        ";
	while ($i < $rows){
		$name = mysql_result($result, $i, "name");
		$id = mysql_result($result, $i, "id");
		echo "<option value=\"$id\"";
		if ($_POST['professor'] == $id)
			echo " selected";
		echo ">$name</option>
		";
		$i++;
	}
}
echo "</select>";

}
?>

 

 

Any help is greatly appreciated (quicker the better, gotta finish this thing this week)...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.