/    Sign up×
Community /Pin to ProfileBookmark

Carrying PHP Variables back to other pages.

I have set up a 4 part form using PHP Sessions. The end result displays results from 4 different pages with forms. All the data on the last page seems fine. However, I have a back option, incase people would like to go back and change an answer.

Example, on page3.php, I have a link back to page2.php.

[CODE]<a href=”page2.php”>Go Back</a>[/CODE]

The user can then change their answer on page2.php, but the answers that were chosen on page1.php seem to get lost.

I am defining each variable as follows

On page2.php:

[CODE]<form action=”page3.php” method=”post”><select name=”style”>
<option value=”style1″>style1</option>
<option value=”style2″>style2</option>
</select>
</form>[/CODE]

And page3.php has the following:

[code=php]$_SESSION[‘style’] = $_POST[‘style’];[/code]

So, say style is on page2.php,

I thought that this would keep each variable registered until it was altered or deleted with session_destroy(); or unset();

Is there something I need to specify to keep these variables?

I have session_start(); on each page.

Thank you

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@ChadAyersNov 16.2009 — One simple way to do this would be to leave these values in the url and use a $_GET instead of a $_POST. But of course it leaves these values exposed in the users address bar.
Copy linkTweet thisAlerts:
@SwoodsauthorNov 16.2009 — Thank you for the idea, however I cannot use GET in this situation. Thanks again for taking the time tho
Copy linkTweet thisAlerts:
@criterion9Nov 16.2009 — Are you adding your responses from page 1 to the session on page 2? From what you posted it looks like you are adding the values from page 2 to the session on page 3 but you didn't post where that happens on page 2.
Copy linkTweet thisAlerts:
@SwoodsauthorNov 17.2009 — The above was just a code snippet. Here's how everything is put together on the various pages.

This is from page1.php

In the <head> section.
[code=php]<?php session_start(); ?>[/code]

Now onto the body
[CODE]<form action="page2.php" method="post">
<select name="size">
<option value="Small">Small</option>
<option value="Medium">Medium</option>
<option value="Large">Large</option>
</select>

<select name="shape">
<option value="Circle">Circle</option>
<option value="Square">Square</option>
</select>

<select name="color">
<option value="White">White</option>
<option value="Black">Black</option>
<option value="Red">Red</option>
</select>

<input type="image" name="submit" id="submit" src="next.jpg" onmouseover="this.src='nexthov.jpg'" onmouseout="this.src='next.jpg'" />
</form>
[/CODE]



On page2.php is the following, in the head section
[code=php]<?php session_start(); ?>
<?php
$_SESSION['size'] = $_POST['size'];
$_SESSION['shape'] = $_POST['shape'];
$_SESSION['color'] = $_POST['color'];
?>[/code]


and then the following in the body
[CODE]<form action="page3.php" method="post">

<select name="option">
<option value="opt1">Option1</option>
<option value="opt2">Option2</option>
<option value="opt3">Option3</option>
</select>

<input type="image" name="submit" id="submit" src="next.jpg" onmouseover="this.src='nexthov.jpg'" onmouseout="this.src='next.jpg'" />
</form>

<a href="page1.php"><img src="back.jpg" width="65" height="55" border="0" /></a>[/CODE]


page3.php has the following in the head section
[code=php]<?php session_start(); ?>

<?php
$_SESSION['option'] = $_POST['option'];
?>
[/code]


And the following in the body
[CODE]<form action="page4.php" method="post">

<select name="secondcol">
<option value="Black">Black</option>
<option value="White">White</option>
<option value="Green">Green</option>
</select>

<input type="image" name="submit" id="submit" src="next.jpg" onmouseover="this.src='nexthov.jpg'" onmouseout="this.src='next.jpg'" />

<a href="page2.php"><img src="back.jpg" width="65" height="55" border="0" /></a>
[/CODE]


That is the basic idea of what I have set up. It seems to work fine, aside from the back button problem. If you go back from page3.php to page2.php using the link, the variables from page1.php seem to disappear.

Any ideas?

Thanks again for taking the time
Copy linkTweet thisAlerts:
@criterion9Nov 17.2009 — You are explicitly setting the session variable even when there is no post data:

$_SESSION['size'] = $_POST['size'];

$_SESSION['shape'] = $_POST['shape'];

$_SESSION['color'] = $_POST['color'];
[/quote]


Try checking first:
[code=php]
if(isset($_POST['size']) && isset($_POST['shape']) && isset($_POST['color'])){
$_SESSION['size'] = $_POST['size'];
$_SESSION['shape'] = $_POST['shape'];
$_SESSION['color'] = $_POST['color'];
}
[/code]


You should do the same on page 3 as well.
Copy linkTweet thisAlerts:
@SwoodsauthorNov 17.2009 — Thank you very much, I will be giving this a try. The if(isset) didn't even occur to me for some reason.

Hopefully this takes care of the problem. You probably saved me a significant amount of time
×

Success!

Help @Swoods 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.18,
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,
)...