/    Sign up×
Community /Pin to ProfileBookmark

Remembering session inputs

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?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 18.2005 — [code=php]
<select name="choosesize">
<?php foreach(array("small","medium") as $val)
{
$select = ($val == $_SESSION['choosesize']) ? " select" : "";
echo "<option value='$val'$select>$val</option>n";
}
?>
</select>
[/code]
Copy linkTweet thisAlerts:
@LocalHeroauthorJun 18.2005 — 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.
Copy linkTweet thisAlerts:
@NogDogJun 19.2005 — A little more robust and for different field types:
[code=php]
<?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";
[/code]

In case you are not familiar with the ternary operators ? and :, the line [b][FONT=Courier New]$val = (isset($_SESSION['field'])) ? $_SESSION['field'] : "";[/FONT][/b] is the same as doing:
[code=php]
if(isset($_SESSION['field']))
{
$val = $_SESSION['field'];
}
else
{
$val = "";
}
[/code]
×

Success!

Help @LocalHero spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...