Hi everyone,

I am stuck on trying to retain the session data when using a function and don't see an answer.

I have a function that I use for drop-down lists. I get the data from my table, assign the name for the list and then display it. The problem is I lose the data if the user goes back (when they hit the back button in the browser or if I use a Javascript back function).

I was sending the data to a results page, and found that its easier to retain the data is I put both form and results in the same page. But I can't get that to work either.

Here is a shortened (not tested - stripped down) example of how I do it.

<?php
session_start();
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Test form.</title>
</head>
<body>
<?php
if (isset($_POST['Submit']) {
$Video = $_POST['Video'];
echo 'You chose the '.$Video;
die();
} else {

include("MySQLconnDB.php");
$videos = array();
$videos[0] = " ";
$dbname = 'database';
mysql_select_db ($dbname, $conn) or die (mysql_error());
$query = "SELECT *
FROM videos";
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_array($result)) {
$videos[] = $row['videoname'];
}
?>
<form action="<?php echo   htmlspecialchars($_SERVER[’PHP_SELF’]); ?>" method="POST" name="form" target="_self">
<?php
$list = makeDropList('Video',$videos,$videos);
echo 'Pick the video --->  '.$list;
?>
<br><br>
<input name="Submit" type="Submit" id="Submit" value="Submit" />&nbsp;&nbsp;
<input name="Reset" type="Reset" id="Reset" value="Reset" />
</form>
<?php } ?>
</body>
</html>

Here is the function for the drop down menus.

<?php
function makeDropList($name,$list,$selected="")
{
  // $name       select name
  // $list       array of value, label pair
  // $selected   selected value
  global $x;
  global $label;
foreach($list as $value=>$label)
  {
  if(($_SESSION[$label] != '') || ($selected == $value)) // Are you sure you store the label?
    {
    $options .= '<option selected value="'.$value.'">'.$label.'</option>';
    }
  else
    {
    $options .= '<option value="'.$value.'">'.$label.'</option>';
    }
  }
  $dropList = '<select name="'.$name.'" tabindex="'.$x.'">'.$options.'</select>'."\n";
  return $dropList;
}
?>

That is a basic version. I know I only have one drop down, but if I used 2 and both were required, but they only chose one, if they tried to go back and chose the 2nd one the first would defualt back.

Is that enough to show where I am going 'south'?

Thanks in advance,

Don

    Write a Reply...