Jump to content

html dropdown in a form, how to set it to show value from session


suttercain

Recommended Posts

HI guys,

 

Here is something I have ignored in the past but would like to understand how to do it.

 

Let's say I have a simple form with a dropdown menu for the user to select an item:

 

<?php session_start(); ?>
<!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>Untitled Document</title>
</head>

<body>
<form action="dropped.php" method="post" />
<select name="dropdown">
<option value="yes">Yes</option>
<option value="no">No</option>
<option value="maybe">maybe</option>
</select>
<input type="submit" name="submit" />
</form>
</body>
</html>

 

Now i submit the form and have it echoing the selection the user made... simple enough:

<?php session_start(); ?>
<!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>Untitled Document</title>
</head>
<?php 
$_SESSION['dropdown'] = $_POST['dropdown'];
echo $_SESSION['dropdown'];
?>
<body>
</body>
</html>

 

So let's say the user selects "maybe", clicks submit and see that "maybe" is echoed to the browser. How do I get it so when the user clicks back in the browser and they are taken back to the form, that "maybe" will be selected in the dropdown menu instead of the defaulted "yes"?

 

Thanks.

<form action="dropped.php" method="post" />
<select name="dropdown">
<option value="yes"<?php echo $_SESSION['dropdown'] == "yes" ? " selected=\"selected\"" : "" ?>>Yes</option>
<option value="no"<?php echo $_SESSION['dropdown'] == "no" ? " selected=\"selected\"" : "" ?>>No</option>
<option value="maybe"<?php echo $_SESSION['dropdown'] == "maybe" ? " selected=\"selected\"" : "" ?>>maybe</option>
</select>
<input type="submit" name="submit" />
</form>

Actually...

 

<form action="dropped.php" method="post" />
<select name="dropdown">
<option value="yes"<?php echo (isset($_SESSION['dropdown']) && $_SESSION['dropdown'] == "yes") ? " selected=\"selected\"" : "" ?>>Yes</option>
<option value="no"<?php echo (isset($_SESSION['dropdown']) && $_SESSION['dropdown'] == "no") ? " selected=\"selected\"" : "" ?>>No</option>
<option value="maybe"<?php echo (isset($_SESSION['dropdown']) && $_SESSION['dropdown'] == "maybe") ? " selected=\"selected\"" : "" ?>>maybe</option>
</select>
<input type="submit" name="submit" />
</form>

 

would be safer.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.