Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2006
    Location
    Charlottesville, VA
    Posts
    94
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Trouble passing session data between 2 form pages

    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/...nd_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 Code:
    <?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

  • #2
    New Coder
    Join Date
    Feb 2006
    Location
    Charlottesville, VA
    Posts
    94
    Thanks
    5
    Thanked 0 Times in 0 Posts
    ... 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 Code:
    <?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.

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    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.

  • #4
    New Coder
    Join Date
    Feb 2006
    Location
    Charlottesville, VA
    Posts
    94
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    You could do it with multiple pages too, and transfer the information via Sessions/Cookies/Hidden form variables.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •