SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict Divisive Cotton's Avatar
    Join Date
    Jun 2008
    Location
    Andy lives in London, UK
    Posts
    393
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Splitting long form into multiple pages

    Is it ever possible to split a long form into multiple pages with the data entered obviously being carried from one page to the next?
    Let everyday be Christmas

  2. #2
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, you can use sessions for this (or if you have to much spare time cookies).
    I just google out this page. Of course have a look at php.net sessions documentation also.

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes. Heres the basic logic.
    PHP Code:
    switch ($_POST['step']) {
        case 
    1:
            
    // first form submitted
            // validate and then show the 2nd form
            // take care to pass values into hidden fields so they are maintained
            
    echo '<form ...>
                  <input type="hidden" name="step" value="2">
                  <input type="hidden" name="firstname" value="' 
    htmlspecialchars($_POST['firstname']) . '">
                  <input name="lastname">
                  </form>'
    ;

        break;
        
        case 
    2:
            
    // second form submitted
            // validate and then show the 3nd form
            // take care to pass values into hidden fields so they are maintained
            
    echo '<form ...>
                  <input type="hidden" name="step" value="3">
                  <input type="hidden" name="firstname" value="' 
    htmlspecialchars($_POST['firstname']) . '">
                  <input type="hidden" name="lastname" value="' 
    htmlspecialchars($_POST['lastname']) . '">
                  <input name="age">
                  </form>'
    ;

        break;

        case 
    3:
            
    //etc....
        
    break;

        default:
            
    // no form submitted
            // show first form
            
    echo '<form ...>
                  <input type="hidden" name="step" value="1">
                  <input name="firstname">
                  </form>'
    ;

    If you have tons of variables to maintain, this can become tedious. sessions can be used, but the functionality will not work well if your user decides to open a second browser window, possibly filling out two forms at a time(it will break). An alternative is to pass an array of variables in a single field, in a serialized format. json_encode() with base64_encode() works well for this.


Bookmarks

Posting Permissions

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