Click to See Complete Forum and Search --> : Remembering session inputs


LocalHero
06-18-2005, 05:26 PM
I'm starting to build an order form that has a five step process. Size, color, quantity, etc. Everything will be added up on the fifth step. I am new to sessions, but I think I can get that part to work, the problem that I can see is this: What if a user wants to change something on step one when they're finished? It's probably simple. I'm thinking that it will be along the lines of...

<?php session_start();
if $_SESSION['choosesize'] is there, then show the size
else, blank selection
?>

<select name="choosesize">
<option value="small">small</option>
<option value="medium">medium</option>
</select>

As you can see I don't know what the code would be. But this idea should work right? Linking 5 php pages together with sessions to be totaled on the final page?

NogDog
06-18-2005, 06:07 PM
<select name="choosesize">
<?php foreach(array("small","medium") as $val)
{
$select = ($val == $_SESSION['choosesize']) ? " select" : "";
echo "<option value='$val'$select>$val</option>\n";
}
?>
</select>

LocalHero
06-18-2005, 06:18 PM
Hmmm... I was hoping I would be able to follow it easily. I kinda get it. I just need to figure out how to modify it for text input, checkbox, and radio. Thanks for the start, I'll try it out for a few hours and see where I get.

NogDog
06-19-2005, 01:22 AM
A little more robust and for different field types:

<?php
# text input:
$val = (isset($_SESSION['field'])) ? $_SESSION['field'] : "";
echo "<input type='text' name='field' value='$val'>\n";

# text area:
$val = (isset($_SESSION['text'])) ? $_SESSION['text'] : "";
echo "<textarea name='text'>$val</textarea>\n";

# select:
echo "<select name='choose'>\n";
foreach(array("choice 1","choice 2","choice 3") as $val)
{
$select = (isset($_SESSION['choose']) and $_SESSION['choose'] == $val) ? " select" : "";
echo "<option value='$val'$select>$val</option>\n";
}
echo "</select>\n";

In case you are not familiar with the ternary operators ? and :, the line $val = (isset($_SESSION['field'])) ? $_SESSION['field'] : ""; is the same as doing:

if(isset($_SESSION['field']))
{
$val = $_SESSION['field'];
}
else
{
$val = "";
}