romram
04-18-2004, 10:46 AM
Hi there....
I have this code where I have two dropdown menus, and I want that when the user select an option from the first dropdown menu , then this value will be used in query to get the options of the second menu...But this code does not do what I expect. :(
<?php
include 'include.inc';
// Connect to a session
session_start();
set_error_handler("errorHandler");
// Show an error in a red font
function fieldError($fieldName, $errors)
{
if (isset($errors[$fieldName]))
echo "<font color=\"red\">" .
$errors[$fieldName] .
"</font><br>";
}
// Initialise $formVars from the $_SESSION["formVars"] (if set)
if (session_is_registered("formVars"))
{
$row = $_SESSION["formVars"];
$formVars["courseName"] = $row["course_name"];
$formVars["sectionNum"] = $row["section_no"];
}
/*******
if (!session_is_registered("formVars"))
session_register("formVars");
foreach($_POST as $varname => $value)
$formVars[$varname] = trim(clean($value, 50));
$_SESSION["formVars"] = $formVars; **********/
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Manage Assignments</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_reloadPage(init) { //reloads the window if Nav4 resized
if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
//-->
</script>
</head>
<body bgcolor="#FFFFe8">
<div id="Layer1" style="position:absolute; left:0px; top:0px; width:508px; height:52px; z-index:1"><img src="logo/eSubmissionbannerBigLighter.jpg" width="783" height="69"></div>
<p> </p>
<p> </p>
<p> </p>
<form name="form1" method="post" action="">
<?php
function selectDistinct ($connection,
$tableName,
$columnName,
$pulldownName,
$additionalOption,
$defaultValue)
{
$defaultWithinResultSet = FALSE;
/* if (!($connection = @ mysql_connect($hostName,
$username,
$password)))
die("Could not connect to database");
if (!mysql_select_db($databaseName))
showerror();*/
if (session_is_registered("loginUsername"))
{
$insID=$_SESSION["loginUsername"];
}
if($pulldownName == "sectionNum")
{
$query= "SELECT * FROM course
WHERE course_name = '".$formVars["courseName"]."'";
echo "###".$courseName;
if (!($courseNo = @ mysql_query ($query,
$connection)))
showerror();
$row = mysql_fetch_array($courseNo);
// Query to find distinct values of $columnName
// in $tableName
$distinctQuery = "SELECT DISTINCT $columnName
FROM $tableName
WHERE inst_id=$insID AND
s.course_no='" . $row["course_no"] . "'";
}//endif
if($pulldownName == "courseName")
$distinctQuery = "SELECT DISTINCT $columnName
FROM $tableName
WHERE inst_id=$insID AND
s.course_no= c.course_no";
// Run the distinctQuery on the databaseName
if (!($resultId = @ mysql_query ($distinctQuery,
$connection)))
showerror();
// Retrieve all distinct values
$i = 0;
while ($row = @ mysql_fetch_array($resultId))
$resultBuffer[$i++] = $row[$columnName];
// Start the select widget
echo "\n<select name=\"$pulldownName\">";
// Is there an additional option?
if (isset($additionalOption))
// Yes, but is it the default option?
if ($defaultValue == $additionalOption)
// Show the additional option as selected
echo "\n\t<option selected>$additionalOption";
else
// Just show the additional option
echo "\n\t<option>$additionalOption";
// check for a default value
if (isset($defaultValue))
{
// Yes, there's a default value specified
// Check if the defaultValue is in the
// database values
foreach ($resultBuffer as $result)
if ($result == $defaultValue)
// Yes, show as selected
echo "\n\t<option selected>$result";
else
// No, just show as an option
echo "\n\t<option>$result";
} // end if defaultValue
else
{
// No defaultValue
// Show database values as options
foreach ($resultBuffer as $result)
echo "\n\t<option>$result";
}
echo "\n</select>";
} // end of function
?>
<table width="75%" border="0" align="center">
<tr>
<td><strong>Select a course:</strong></td>
<td>
<?php // Connect to the DBMS
if (!($connection = @ mysql_connect($hostName,
$username,
$password)))
die("Could not connect to database");
if (!mysql_select_db($databaseName))
showerror();
// Produce the select list
// Parameters:
// 1: Database connection
// 2. Table that contains values
// 3. Attribute that contains values
// 4. <SELECT> element name
// 5. An additional non-database value
// 6. Optional <OPTION SELECTED>
selectDistinct ($connection,
"section s, course c",
"course_name",
"courseName",
"",
"");
?>
</td>
</tr>
<tr>
<td><strong>Select a section:</strong></td>
<td>
<?php // Connect to the DBMS
/*if (!($connection = @ mysql_connect($hostName,
$username,
$password)))
die("Could not connect to database");
if (!mysql_select_db($databaseName))
showerror();*/
// Produce the select list
// Parameters:
// 1: Database connection
// 2. Table that contains values
// 3. Attribute that contains values
// 4. <SELECT> element name
// 5. An additional non-database value
// 6. Optional <OPTION SELECTED>
selectDistinct($connection,
"section s, course c",
"section_no",
"sectionNum",
"",
"");
?>
</td>
</tr>
<tr>
<td height="21" colspan="2"><div align="center"> <a href="inst_view_assig.php"><img src="button/t_mangAss_viewAssi.jpg" width="250" height="49" border="0"></a></div></td>
</tr>
<tr>
<td height="21" colspan="2"><div align="center"><strong>or</strong></div></td>
</tr>
<tr>
<td height="21" colspan="2"><div align="center"> <a href="inst_add_assig.php"><img src="button/t_mangAss_addAssi.jpg" width="250" height="49" border="0"></a></div></td>
</tr>
<tr>
<td height="21" colspan="2"><div align="center"><strong>or</strong></div></td>
</tr>
<tr>
<td height="21" colspan="2"><div align="center"> <img src="button/t_mangAss_gradesRec.jpg" width="250" height="49"></div></td>
</tr>
</table>
</form>
<p> </p>
</body>
</html>
I have this code where I have two dropdown menus, and I want that when the user select an option from the first dropdown menu , then this value will be used in query to get the options of the second menu...But this code does not do what I expect. :(
<?php
include 'include.inc';
// Connect to a session
session_start();
set_error_handler("errorHandler");
// Show an error in a red font
function fieldError($fieldName, $errors)
{
if (isset($errors[$fieldName]))
echo "<font color=\"red\">" .
$errors[$fieldName] .
"</font><br>";
}
// Initialise $formVars from the $_SESSION["formVars"] (if set)
if (session_is_registered("formVars"))
{
$row = $_SESSION["formVars"];
$formVars["courseName"] = $row["course_name"];
$formVars["sectionNum"] = $row["section_no"];
}
/*******
if (!session_is_registered("formVars"))
session_register("formVars");
foreach($_POST as $varname => $value)
$formVars[$varname] = trim(clean($value, 50));
$_SESSION["formVars"] = $formVars; **********/
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Manage Assignments</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_reloadPage(init) { //reloads the window if Nav4 resized
if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
//-->
</script>
</head>
<body bgcolor="#FFFFe8">
<div id="Layer1" style="position:absolute; left:0px; top:0px; width:508px; height:52px; z-index:1"><img src="logo/eSubmissionbannerBigLighter.jpg" width="783" height="69"></div>
<p> </p>
<p> </p>
<p> </p>
<form name="form1" method="post" action="">
<?php
function selectDistinct ($connection,
$tableName,
$columnName,
$pulldownName,
$additionalOption,
$defaultValue)
{
$defaultWithinResultSet = FALSE;
/* if (!($connection = @ mysql_connect($hostName,
$username,
$password)))
die("Could not connect to database");
if (!mysql_select_db($databaseName))
showerror();*/
if (session_is_registered("loginUsername"))
{
$insID=$_SESSION["loginUsername"];
}
if($pulldownName == "sectionNum")
{
$query= "SELECT * FROM course
WHERE course_name = '".$formVars["courseName"]."'";
echo "###".$courseName;
if (!($courseNo = @ mysql_query ($query,
$connection)))
showerror();
$row = mysql_fetch_array($courseNo);
// Query to find distinct values of $columnName
// in $tableName
$distinctQuery = "SELECT DISTINCT $columnName
FROM $tableName
WHERE inst_id=$insID AND
s.course_no='" . $row["course_no"] . "'";
}//endif
if($pulldownName == "courseName")
$distinctQuery = "SELECT DISTINCT $columnName
FROM $tableName
WHERE inst_id=$insID AND
s.course_no= c.course_no";
// Run the distinctQuery on the databaseName
if (!($resultId = @ mysql_query ($distinctQuery,
$connection)))
showerror();
// Retrieve all distinct values
$i = 0;
while ($row = @ mysql_fetch_array($resultId))
$resultBuffer[$i++] = $row[$columnName];
// Start the select widget
echo "\n<select name=\"$pulldownName\">";
// Is there an additional option?
if (isset($additionalOption))
// Yes, but is it the default option?
if ($defaultValue == $additionalOption)
// Show the additional option as selected
echo "\n\t<option selected>$additionalOption";
else
// Just show the additional option
echo "\n\t<option>$additionalOption";
// check for a default value
if (isset($defaultValue))
{
// Yes, there's a default value specified
// Check if the defaultValue is in the
// database values
foreach ($resultBuffer as $result)
if ($result == $defaultValue)
// Yes, show as selected
echo "\n\t<option selected>$result";
else
// No, just show as an option
echo "\n\t<option>$result";
} // end if defaultValue
else
{
// No defaultValue
// Show database values as options
foreach ($resultBuffer as $result)
echo "\n\t<option>$result";
}
echo "\n</select>";
} // end of function
?>
<table width="75%" border="0" align="center">
<tr>
<td><strong>Select a course:</strong></td>
<td>
<?php // Connect to the DBMS
if (!($connection = @ mysql_connect($hostName,
$username,
$password)))
die("Could not connect to database");
if (!mysql_select_db($databaseName))
showerror();
// Produce the select list
// Parameters:
// 1: Database connection
// 2. Table that contains values
// 3. Attribute that contains values
// 4. <SELECT> element name
// 5. An additional non-database value
// 6. Optional <OPTION SELECTED>
selectDistinct ($connection,
"section s, course c",
"course_name",
"courseName",
"",
"");
?>
</td>
</tr>
<tr>
<td><strong>Select a section:</strong></td>
<td>
<?php // Connect to the DBMS
/*if (!($connection = @ mysql_connect($hostName,
$username,
$password)))
die("Could not connect to database");
if (!mysql_select_db($databaseName))
showerror();*/
// Produce the select list
// Parameters:
// 1: Database connection
// 2. Table that contains values
// 3. Attribute that contains values
// 4. <SELECT> element name
// 5. An additional non-database value
// 6. Optional <OPTION SELECTED>
selectDistinct($connection,
"section s, course c",
"section_no",
"sectionNum",
"",
"");
?>
</td>
</tr>
<tr>
<td height="21" colspan="2"><div align="center"> <a href="inst_view_assig.php"><img src="button/t_mangAss_viewAssi.jpg" width="250" height="49" border="0"></a></div></td>
</tr>
<tr>
<td height="21" colspan="2"><div align="center"><strong>or</strong></div></td>
</tr>
<tr>
<td height="21" colspan="2"><div align="center"> <a href="inst_add_assig.php"><img src="button/t_mangAss_addAssi.jpg" width="250" height="49" border="0"></a></div></td>
</tr>
<tr>
<td height="21" colspan="2"><div align="center"><strong>or</strong></div></td>
</tr>
<tr>
<td height="21" colspan="2"><div align="center"> <img src="button/t_mangAss_gradesRec.jpg" width="250" height="49"></div></td>
</tr>
</table>
</form>
<p> </p>
</body>
</html>