...

View Full Version : save drop down value in session variable



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>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</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>&nbsp;</p>
</body>
</html>

black3842
04-18-2004, 12:01 PM
I didn't trace through all of your code, because frankly the formatting and indentation is rather hard to follow, but I think there may be a basic logic problem. PHP is rendered before it goes to the browser, so interaction with the user is not something you can do with php unless you want to make 2 passes, Display first drop down, post selection, display second drop down based on posted selection. Problem is, once you make selection on client browser, can't get that selection processed by php unless you post it back to the server, since php isn't client side.....

You can't do it in one pass...not with php, at least I don't think, I'm rather new to php, but that is my current understanding.

- I reserve the right to be wrong....

Regards,

romram
04-18-2004, 01:48 PM
can you give me a hint??

black3842
04-20-2004, 01:39 AM
You could try something like this...


//See if firstSelection has been made
if (isset($_POST['firstSelection']))
{
put code to display/populate second drop down here
based on value in $_POST['firstSelection']
}
else //if not, display dropdown to make firstSelection
{
code to display first selection box.
post firstSelection to this page
}

This is what I mean by 2 passes....
Page loads first time, firstSelection post variable is not set,
so we else to display first drop down box.

Selection is made in browser and posted back to server..posted to same page.

Page loads again, this time firsSelection post variable isset so we
can use the value in firstselection to do query and populate code in second dropdown box.
-- hope this helps

PS, you don't have to post to the same page, you could use different pages....just easier for the example.

romram
04-21-2004, 06:27 AM
how can I let the user select the first selection and then post the page back to the server?

black3842
04-22-2004, 05:39 AM
try something like this....substituting your own values of course.


<HTML>
<BODY>
<form enctype='multipart/form-data' METHOD=POST ACTION="YourPage.php">
<select name="firstSelection" >
<option value="Value1">Value 1</option>
<option value="Value2">Value 2</option>
<option value="Value3">Value 3</option>
</select>
<input type=submit value='Make Selection'>
</form>
</BODY>
</HTML>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum