Thread: Multi-page form

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    16
    Rep Power
    0

    Multi-page form


    Hello guys! I've been trying to create a multi-page form for way too long now, with no real success...

    What i'm trying to achieve is something like this gravitywiz.com/demos/multi-page-navigation/ - the user will be asked several questions over 3 pages and on the 4th page the user can view his/her entries. I also would like the user to be able to go back to a certain page to edit the results.

    Once the results appear correct the user will click confirm which will send the results to my database.

    I would like it create it in PHP!

    If anyone can point me in the right direction it would be much appreciated! I'm no PHP guru so any help at all is useful.

    Thanks,
    Luke.
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    You would use the session to hold the information while the user browses the site. For each page, check to see if there are answers already present in the session for each form field. If so, print them into the field. If not, print the field empty. When each page is submitted, store the answers in the session and move on to the next page. At the end, print out the contents of the session for each page. When they want to "go back" to previous pages, your forms will auto-fill with the information they had previously entered (if you did what I said above).
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    16
    Rep Power
    0
    Originally Posted by ManiacDan
    You would use the session to hold the information while the user browses the site. For each page, check to see if there are answers already present in the session for each form field. If so, print them into the field. If not, print the field empty. When each page is submitted, store the answers in the session and move on to the next page. At the end, print out the contents of the session for each page. When they want to "go back" to previous pages, your forms will auto-fill with the information they had previously entered (if you did what I said above).
    Thanks for your reply!

    Something like this?
    Page 1:
    PHP Code:
    <?php session_start(); $customer_name $_POST['customer_name']; $_SESSION['varname'] = $var_value?> <html> <body> <form action="page2.php" method="post"> <label>Name</label> <input name="customer_name" type="text" id="customer_name" value= "<?php echo $customer_name ?>"> <input type="submit" value="next"> </form> </body></html>
    Page 2:
    PHP Code:
    <?php session_start(); $customer_name $_POST['customer_name']; $surname $_POST['surname']; ?> <html> <body> <form action="page3.php" method="post"> <label>surname</label> <input name="surname" class="surname" type="text" id="surname" value= "<?php echo $surname ?>"> <input type="submit" value="next"> </form> </body></html>
    Page 3:
    PHP Code:
    <?php  $customer_name $_POST['customer_name']; $surname $_POST['surname']; ?> <html> <body> <form action="" method="post"> <table border="1"> <tr> <td>First Name</td> <td>Surname</td> </tr> <tr> <td><span class="td-title"><?php echo $customer_name ?></span></td> <td><span class="td-ends"><?php echo $surname ?></span></td> </tr> </table> </form></body></html>
    With this code i get error "Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent" and the input from page1 isn't displayed?
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Paste your code into the box, then highlight it and click the white "PHP" button. your code is one long line.

    The cache header error comes from you having output before you call session_start. You may have NOTHING before your <?PHP tags. Seriously nothing. Zero things. I want to be clear on the definition of "nothing" because so many people don't understand when I say nothing. If there is something before <?PHP, get rid of it.

    You also can't echo/print anything before session_start, nor can you have any errors in your code which would throw warnings to the screen.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    16
    Rep Power
    0
    Originally Posted by ManiacDan
    Paste your code into the box, then highlight it and click the white "PHP" button. your code is one long line.

    The cache header error comes from you having output before you call session_start. You may have NOTHING before your <?PHP tags. Seriously nothing. Zero things. I want to be clear on the definition of "nothing" because so many people don't understand when I say nothing. If there is something before <?PHP, get rid of it.

    You also can't echo/print anything before session_start, nor can you have any errors in your code which would throw warnings to the screen.
    Ah yes, the error was because the code started on line 2.

    Page 1:
    PHP Code:
    <?php session_start();

    $customer_name $_POST['customer_name'];
    $_SESSION['varname'] = $var_value;
    ?>
    <html>
    <body>
    <form action="page2.php" method="post">

    <label>Name</label>
    <input name="customer_name" type="text" id="customer_name" value= "<?php echo $customer_name ?>">
    <input type="submit" value="next">
    </form>
    </body></html>
    Page 2:
    PHP Code:
    <?php session_start();

    $customer_name $_POST['customer_name'];
    $surname $_POST['surname'];
    ?>
    <html>
    <body>
    <form action="page3.php" method="post">

    <label>surname</label>
    <input name="surname" class="surname" type="text" id="surname" value= "<?php echo $surname ?>">
    <input type="submit" value="next">
    </form>
    </body></html>
    Page 3:
    PHP Code:
    <?php session_start();
    $customer_name $_POST['customer_name'];
    $surname $_POST['surname'];
    ?>
    <html>
    <body>
    <form action="" method="post">

    <table border="1">
    <tr>
    <td>First Name</td>
    <td>Surname</td>
    </tr>
    <tr>
    <td><span class="td-title"><?php echo $customer_name ?></span></td>
    <td><span class="td-ends"><?php echo $surname ?></span></td>
    </tr>
    </table>

    </form></body></html>
    Thanks,
    Luke
  10. #6
  11. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Originally Posted by luke13
    Something like this?
    More like:

    Page 1:
    PHP Code:
    // Display form to collect customer name, POST to page 2 
    Page 2:
    PHP Code:
    <?php
        session_start
    ();
        
    $customer_name $_POST['customer_name'];
        
    $_SESSION['customer_name'] = $customer_name;
    ?>
    // Display form to collect surname, POST to page 3
    Page 3:
    PHP Code:
    <?php
        $customer_name 
    $_SESSION['customer_name'];
        
    $surname $_POST['surname'];
        
    $_SESSION['surname'] = $surname// For consistency
    ?>
    // Display confirmation page with $customer_name and $surname
    There are some other ways to model this that might be better, but this should give you the gist of using $_SESSION.

    With this code i get error "Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent" and the input from page1 isn't displayed?
    Then somehow, somewhere, PHP is outputting something before you do session_start(). If your code is exactly as you describe, that shouldn't be happening. So I'm guessing there's something else.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    16
    Rep Power
    0
    Originally Posted by dmittner
    More like:
    I fixed the error - it was because the code started on line 2. I'm still not able to get the input from page1 onto page 3?

    Page1:
    PHP Code:
    <html>
    <body>
    <form action="page2.php" method="post">

    <label>Name</label>
    <input name="customer_name" type="text" id="customer_name" value= "<?php echo $customer_name ?>">
    <input type="submit" value="next">
    </form>
    </body></html>
    Page2:
    PHP Code:
    <?php 
        session_start
    (); 
        
    $customer_name $_POST['customer_name']; 
        
    $_SESSION['customer_name'] = $customer_name
    ?> <html>
    <body>
    <form action="page3.php" method="post">

    <label>surname</label>
    <input name="surname" class="surname" type="text" id="surname" value= "<?php echo $surname ?>">
    <input type="submit" value="next">
    </form>
    </body></html>
    Page3:
    PHP Code:
    <?php 
        $customer_name 
    $_SESSION['customer_name']; 
        
    $surname $_POST['surname']; 
        
    $_SESSION['surname'] = $surname// For consistency 
    ?> 
    <html>
    <body>
    <form action="" method="post">

    <table border="1">
    <tr>
    <td>First Name</td>
    <td>Surname</td>
    </tr>
    <tr>
    <td><span class="td-title"><?php echo $customer_name ?></span></td>
    <td><span class="td-ends"><?php echo $surname ?></span></td>
    </tr>
    </table>

    </form></body></html>
    Thanks
  14. #8
  15. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Neither one is correct, as $_POST is not necessarily filled.

    All three pages should take the form:

    PHP Code:
    <?php

    session_start
    ();

    //current page name here, must be the filename without '.php'
    $pageName 'Page2';
    //previous page name here, must be the filename without '.php'
    $prevPage 'Page1';
    //next page name here, must be the filename without '.php'
    $nextPage 'Page3';


    //for later - errors from the previous page
    $errors = array();

    //process any posted data from the previous page and store in the session
    if ( isset($_POST['submit']) ) {
      foreach ( 
    $_POST[$prevPage] as $name => $value ) {
        
    //right here, put any special processing rules for validation or whatever you need.  For example:
        
    if ( $name == 'email' ) {
          if ( 
    $value != filter_var($valueFILTER_VALIDATE_EMAIL) ) {
            
    $errors[$name] = 'Email address invalid';
          }
        }
        
    //end filter/errors example
        
        //add the data in POST to the session
        
    $_SESSION[$prevPage][$name] = $value;
      }
    }

    //if errors, redirect to the previous page:
    if ( count($errors) ) {
      unset(
    $_SESSION['errors']);
      
    $_SESSION['errors'] = $errors;
      
    header('Location: ' $prevPage '.php');
      die();
    }

    //form processing completed.  print the form for this page.
    ?>
    <form action="<?=$nextPage?>.php" method="POST">

      Billing Address 1 <input name='<?=$pagename?>[billAddr1]' value="<?=isset($_SESSION[$pagename]['billAddr1']) ? $_SESSION[$pagename]['billAddr1'] : ''?>" /> 
        <span class="error"><?=isset($_SESSION['errors']['billAddr1'] ? $_SESSION['errors']['billAddr1'] : ''?></span>
        
        
      <!-- etc. -->
      
      <input type="Submit" name="submit" value="Next Page" />  
    </form>
    The final page should process errors from the last page (redirect if necessary), then print out everything to confirm. The "confirmation complete" page should loop through the session and safe everything.

    This isn't how I'd design this, I'd set up all the forms as a big nested array and use one large page as the stepper but this is a general idea.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  16. #9
  17. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Originally Posted by luke13
    I fixed the error - it was because the code started on line 2. I'm still not able to get the input from page1 onto page 3?
    Sorry, that was my bad. You need "session_start();" at the beginning of page 3. I forgot that in my example.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    16
    Rep Power
    0
    Originally Posted by dmittner
    Sorry, that was my bad. You need "session_start();" at the beginning of page 3. I forgot that in my example.
    Thanks for that! I've actually tried using arrays but i couldn't get it to work?

    Page 1
    PHP Code:
    <?php session_start();

    foreach(
    $_POST as $k=>$v) {
    $_SESSION['post'][$k]=$v;
    }

     
    ?>

    <form action="page2.php" method="post" enctype="multipart/form-data">
    <fieldset>
    <label for="first">First Name</label>
    <input name="first" id="first" type="text" size="25" />
    <br />
    <label for="last">Last Name</label>
    <input name="last" id="last" type="text" size="25" />
    <br />
    <input name="Go" type="submit" value="Go" />
    </fieldset>
    </form>
    Page 2
    PHP Code:
    <?php session_start();

    foreach(
    $_POST as $k=>$v) {
    $_SESSION['post'][$k]=$v;
    }
     
    ?>
    <form action="page3.php" method="post" enctype="multipart/form-data">
    <fieldset>
    <label for="color">Color</label>
    <input name="color" id="color" type="text" size="25" />
    <br />
    <input name="Go" type="submit" value="Go" />
    </fieldset>
    </form>
    Page 3
    PHP Code:
    <?php session_start();
    foreach(
    $_POST as $k=>$v) {
    $_SESSION['post'][$k]=$v;
    }
     
    ?>
    <html> 
    <body> 
    <form action="" method="post"> 

    <table border="1"> 
    <tr> 
    <td>First Name</td> 
    <td>Surname</td> 
    <td>Color</td> 
    </tr> 
    <tr> 
    <td><span class="td-title"><?php echo $first ?></span></td> 
    <td><span class="td-ends"><?php echo $last ?></span></td> 
    <td><span class="td-ends"><?php echo $color ?></span></td> 

    </tr> 
    </table> 

    </form></body></html>
  20. #11
  21. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Anybody notice my fully-formed example above? With error handling and everything?
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  22. #12
  23. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Originally Posted by ManiacDan
    Neither one is correct, as $_POST is not necessarily filled.
    Well, if my goal was to make it perfect I'd have done a lot more than I did, including killing the multiple pages in favor of one controller capable of updating all variables, and leveraging templates to display the different "steps".

    I was just showing him where and how to interact with the session.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    16
    Rep Power
    0
    Originally Posted by ManiacDan
    Neither one is correct, as $_POST is not necessarily filled.

    All three pages should take the form:

    PHP Code:
    <?php

    session_start
    ();

    //current page name here, must be the filename without '.php'
    $pageName 'Page2';
    //previous page name here, must be the filename without '.php'
    $prevPage 'Page1';
    //next page name here, must be the filename without '.php'
    $nextPage 'Page3';


    //for later - errors from the previous page
    $errors = array();

    //process any posted data from the previous page and store in the session
    if ( isset($_POST['submit']) ) {
      foreach ( 
    $_POST[$prevPage] as $name => $value ) {
        
    //right here, put any special processing rules for validation or whatever you need.  For example:
        
    if ( $name == 'email' ) {
          if ( 
    $value != filter_var($valueFILTER_VALIDATE_EMAIL) ) {
            
    $errors[$name] = 'Email address invalid';
          }
        }
        
    //end filter/errors example
        
        //add the data in POST to the session
        
    $_SESSION[$prevPage][$name] = $value;
      }
    }

    //if errors, redirect to the previous page:
    if ( count($errors) ) {
      unset(
    $_SESSION['errors']);
      
    $_SESSION['errors'] = $errors;
      
    header('Location: ' $prevPage '.php');
      die();
    }

    //form processing completed.  print the form for this page.
    ?>
    <form action="<?=$nextPage?>.php" method="POST">

      Billing Address 1 <input name='<?=$pagename?>[billAddr1]' value="<?=isset($_SESSION[$pagename]['billAddr1']) ? $_SESSION[$pagename]['billAddr1'] : ''?>" /> 
        <span class="error"><?=isset($_SESSION['errors']['billAddr1'] ? $_SESSION['errors']['billAddr1'] : ''?></span>
        
        
      <!-- etc. -->
      
      <input type="Submit" name="submit" value="Next Page" />  
    </form>
    The final page should process errors from the last page (redirect if necessary), then print out everything to confirm. The "confirmation complete" page should loop through the session and safe everything.

    This isn't how I'd design this, I'd set up all the forms as a big nested array and use one large page as the stepper but this is a general idea.
    Thanks! I keep getting this error - Invalid argument supplied for foreach() on line 18?
  26. #14
  27. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Originally Posted by luke13
    Thanks! I keep getting this error - Invalid argument supplied for foreach() on line 18?
    Towards the end, in the HTML:
    PHP Code:
    <?=$pagename?>
    That should be $pageName.

    I think that's all that's needed to get it working...
  28. #15
  29. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Thanks dmittner. I typed that all out in one shot without any debugging or testing, so it's not guaranteed to work perfectly, it's a framework.

    Also, like both of us said, the proper way to do this would be to have one page accept a "page number" argument and work from there, not make it 4 separate files with mostly identical code.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo