...

View Full Version : Trouble passing session data between 2 form pages



Opally
08-10-2007, 06:29 PM
Form part 1.
Depending on the selection in a field which controls the form action, the user goes to either review-1, or part 2.
The review-1 page shows the data from part 1, offers option to return and edit, or submit.
All that works fine.

Form part 2 (optional)
One fills out additional fields, which are all appended to the same session. I know that the session data from part 1 is maintained into form page part 2 because I can display it on that page. However, when one advances to review-2, the part 1 data is gone. That's the problem.

You can see the forms here: http://www.coreknowledge.org/secure/html_forms/friend_initial.php

I think the issue is with how I handle my session on each page. When I go to a review page, I unset and reset the session so that if there were any edits, the display is updated.

I think that, however, if the inputs aren't happening on that page, the unset deletes all that info instead of updating it.

Beginning of a review page:


<?php
session_start();
include('./includes/regisform_select_options.php');

define('MAGIC_QUOTES', get_magic_quotes_gpc());

if( !empty($_POST) ) {
/**
* So let's delete the $_SESSION and add the latest information again.
**/
unset($_SESSION['friendinit']);
foreach($_POST as $key => $value) {
$_SESSION['friendinit'][$key] = ( (MAGIC_QUOTES) ? trim(strip_tags(stripslashes($value))) : trim(strip_tags($value)) );
}
} else {
/* They directly accessed this page so we send them back */
header('Location: friend_initial.php');
exit;
}
?>

Not sure how to handle this. Maintain the data as hidden fields? Some smarter way to handle the session?

thanks for your ideas and pointers.

---Opally

Opally
08-10-2007, 08:07 PM
... maybe something like this...

I found this nice snippet on working with multipage forms in a book called PHP Cookbook, so I'm trying to work that out with what I've got so far.


<?php
// turn on sessions
session_start();

// figure out what staget to use
if (($_SERVER['REQUEST_METHOD'] == 'GET') || (! isset($_POST['stage']))) {
$stage = 1;
} else {
$stage = (int)$_POST['stage'];
}

// Save any submitted data
if ($stage > 1) {
foreach ($_POST as $key => $value) {
$_SESSION[$key] = $value;
}
}

if ($stage == 1) { ?>

<form action = '<?php echo $_SERVER['SCRIPT_NAME'] ?>' method = 'post'>
/* form fields here */

<input type='hidden' name='stage' value='<?php echo $stage + 1?>'>
</form>

<?php } else if ($stage == 2) { ?>

<form action = '<?php echo $_SERVER['SCRIPT_NAME'] ?>' method = 'post'>
/* form fields here */

<input type='hidden' name='stage' value='<?php echo $stage + 1?>'>
</form>

<?php } else if ($stage == 3) { ?>

/* form fields here */

<?php } ?>

I'm not clear how these stages can bring up different pages... maybe add a header('Location: --'); ? But doesn't that interfere with the execution of the script... or am I being dumb, the script is already executing from server memory after it's read the first time?

ciao.

Inigoesdr
08-10-2007, 10:34 PM
That script assumes that you're POSTing the form each time the page is loaded, and it sends the next stage number as a hidden html form variable. So, while it's using the same php file, when the php is parsed each time the form is submitted you see a different page.

Opally
08-13-2007, 04:03 PM
ah, thanks for the reply, Inigoesdr. If I understand, then, if I'm using multiple php pages (not loading each page from the same PHP script) then this won't work, correct?

I did make progress by removing the unset $_SESSION command.

Inigoesdr
08-14-2007, 12:46 AM
You could do it with multiple pages too, and transfer the information via Sessions/Cookies/Hidden form variables.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum