Forum Moderators: coopster

Message Too Old, No Replies

Multi Page Forms and Session Variables

         

TymArtist

4:11 pm on Nov 14, 2005 (gmt 0)

10+ Year Member



Here's one for you guys...

I'm attempting to construct a multi-page form - let's just say there's two pages of form, and one final page that displays all the results. In addition, the results would be displayed on the second page to verify that they have been entered.

As I've been told and from what I've read, the easiest way of accomplishing this is through sessions. Don't try to convince me otherwise, as my pages are so close I can feel it working :). I've gone through a few tutorials on this, including books and online resources. Obviously, that final step is something I'm going to need some help on.

My first note is that if I attempt to use any session_register("variable"); features on these pages, the form doesn't pass it to the next page. However, it works perfectly well if i session_register("variable"); and then do a $variable = "value"; - passes to all the pages in the session. Unfortunately, that's not quite what I'm trying to accomplish here...

So without further adieu...

On the top of each page:
------
<?php
session_start();
?>
------

Then on php-test1.html and php-test2.html, a form like this. The .htaccess file is set to parse PHP, hence the .html file name:

------
<form method="post" action="php-test2.html">
<div class="form"><div>
<label for="last">First Name</label> <input name="first" />
<label for="last">Last Name</label> <input name="last" />
<label for="email">Email</label> <input name="email" />
<label for="submit"></label>
<input type="submit" name="submit" value="Submit" />
------

Then I've attempted to call the variables on php-test2.html submitted from php-test1.html, and on the php-test3.html submitted from php-test1.html and php-test2.html.

If I use the following method of viewing the session data, it does carry from php-test1.html to php-test2.html and php-test2.html to phptest3.html. However, none of the data from php-test1.html is ever carried over to phptest3.html...

------
<p>Name: <strong><?=$first?> <?=$last?></strong></p>
<p>Email: <strong><?=$email?></strong></p>
------

As a last ditch effort, I tried this on php-test2.html but it doesn't seem to call anything at all...

------
<?php
echo $_SESSION['first'];
echo $_SESSION['last'];
?>
------

Let me know if you have any tips, links, or other materials that might help me out. I've checked out the session information on PHP.net and followed the instructions as they were given to me from other places, and in the end it's probably a huge newb error. But I'm somewhat new to this language, so any help is very much appreciated :).

coopster

4:41 am on Nov 18, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The variables should carry from page to page, as you suspect, but your problem may lie in using session_register() [php.net]. I never use it. Start the session, just as you are, in each script that you want to use $_SESSION variables and either read, write or update the variables accordingly.

For example, in the second page you might do something like ...

$session_start(); 
$_SESSION['lastname'] = $_POST['lastname']);

Then in page 3 you would start the session again and you could read that saved variable from your session data.

Hopefully I read your post and understood you correctly ;)