DEM
DEM

Reputation: 333

Can't pass PHP session variables to multiple pages

so i am having a problem passing php session variables to multiple pages.

Essentially I have 1 form that collects answers. upon submission the user is taken to a page where dependent on the choice they made in the form, they will see a page loaded in an iframe. I have figured out how to make it work from form submission to the first page but when i try to carry over the variables to another page (via hyperlink) they do not stay.

below is my code:

form

<form method="post" action="offers.php"> 

0. <input type="text" name="name0"/> <br/><br/>
1. <input type="text" name="name1"/> <br/><br/>
<input type="submit" name="submit"/> </form>

Page 1 (after form submission offers.php)

<?php 
session_start(); 

$_SESSION['name0'] = $_POST['name0'];
$_SESSION['name1'] = $_POST['name1'];


$name0 = $_POST['name0'];

$name1 = $_POST['name1'];


if ($name0 == 'dave')
$site="offer1.php";
elseif ($name0 == 'john')
$site="offer1a.php";
else
$site="http://websiteC.com";

?>

<HTML>
<body>
<div style="height:90px;">header</div>
<iframe src="<?php echo $site; ?>" name="offerFrame" style="width:100%; height:100%;"     align="center"></iframe>
<br>
<a href="offer2.php">dfsdf</a>


</body>   
</HTML>

page2 (user gets here via hyperlink - offer2.php)

<?php 
session_start(); 

$_SESSION['name0'] = $_POST['name0'];
$_SESSION['name1'] = $_POST['name1'];


$name0 = $_POST['name0'];

$name1 = $_POST['name1'];


if ($name1 == 'dave')
$site="2a.php";
elseif ($name1 == 'john')
$site="2b.php";
else
$site="http://websiteC.com";

?>

<HTML>
<body>
<div style="height:90px;">header</div>
<iframe src="<?php echo $site; ?>" name="offerFrame" style="width:100%; height:100%;"  align="center"></iframe>
<br>
<a href="offer3.php">dfsdf</a>


</body>
</HTML>

Thanks for the help

-dave

Upvotes: 2

Views: 18845

Answers (3)

Boldewyn
Boldewyn

Reputation: 82814

In page2 you overwrite the session variables with not existing post variables. Change this:

$_SESSION['name0'] = $_POST['name0'];
$_SESSION['name1'] = $_POST['name1'];


$name0 = $_POST['name0'];

$name1 = $_POST['name1'];

to this:

$name0 = $_SESSION['name0'];

$name1 = $_SESSION['name1'];

Upvotes: 1

jeroen
jeroen

Reputation: 91792

This is your problem in page 2:

$_SESSION['name0'] = $_POST['name0'];
$_SESSION['name1'] = $_POST['name1'];

The user gets there via a hyperlink, so the $_POST array is empty and you are overwriting your session variables with empty ones.

You can get the data via the session variables:

$name0 = $_SESSION['name0'];
// etc.

Upvotes: 1

MattP
MattP

Reputation: 2863

You only assign a session variable once, so page 1 is correct, then is is available for the whole session, uses session_start() at the top of each page.

page2 (user gets here via hyperlink - offer2.php)

<?php 
session_start(); 

$name0 = $_SESSION['name0'];
$name1 = $_SESSION['name1'];

Upvotes: 2

Related Questions