SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Zealot pacman2504's Avatar
    Join Date
    Oct 2003
    Location
    North Wales
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to properly maintain form values?

    Hi

    I need a bit of advice with a six page form that I'm coding.

    I want to have a sort of breadcrumbs-style list at the top of the page with links to Page 1 Page 2 Page 3 etc., and all pages of the form echo the session variable for that particular field, so that if a user needs to go back a couple of pages the form values will not be lost.

    However, in order that the user must be forced to start from Page 1, I have a condition on Pages 2-6 that if Page 1 hasn't been submitted the user is returned automatically to Page 1.

    Now, if I use the links as above to navigate back to Page 2 from, say, Page 4, the user will automatically be returned to Page 1.

    So do I need a different way of preventing the user from accessing subsequent pages if Page 1 hasn't been submitted, or is there a better way to provide navigation through the pages without losing form values?
    In the land of the witless, the half-wit is king.
    Business and liability insurance quotes

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    set page 1 submit as a session variable..
    PHP Code:
    if(isset($_POST['page_form_1'])) {
    $_SESSION['page_1_complete'] = 'Y';
    }
    #etc 
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Zealot ceefour's Avatar
    Join Date
    Feb 2005
    Location
    Bandung, Indonesia
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "...all pages of the form echo the session variable..."
    --
    Why would you echo the session variables? Session variables are "permanent" (actually temporarily persistent) so actually you can use them without even putting ANYthing into your forms. Hell even you can just use an empty form or even access another part of your site and the session variables would still be there. Why not just use session variables for storing these submitted form variables?

    You'll want to set the session expire and garbage collection to a longer period though, to make sure nobody loses their data even after extended periods... (if somebody doesn't complete your 5-page wizard in 10 days, well... I guess you can delete them anyway).

  4. #4
    SitePoint Zealot pacman2504's Avatar
    Join Date
    Oct 2003
    Location
    North Wales
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As far as I can see, I've already done that. This is the code I have used on all pages:
    PHP Code:
    if ($_SESSION['session'] != "active") {
      
    header("Location: page1.php");

    Yet if I navigate from page 6 to page 3, for example, I keep getting thrown back to page 1.
    In the land of the witless, the half-wit is king.
    Business and liability insurance quotes

  5. #5
    SitePoint Zealot pacman2504's Avatar
    Join Date
    Oct 2003
    Location
    North Wales
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ceefour,

    Perhaps I haven't explained myself well enough.

    I understand that session variables will persist for the duration of the session, but when I said that I echo all of the form variables, I meant in this manner:
    PHP Code:
    <input type="text" name="example" value="<?echo $_SESSION['example'];?>" />
    So if the user returns to this page, the value that they have entered, and which is stored as a session variable, will display as the value of that particular field.

    Is there a better way of doing this, so that the user does not have to re-input all details?
    In the land of the witless, the half-wit is king.
    Business and liability insurance quotes

  6. #6
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Is the session 'active' being set?
    Use print_r($_SESSION) to find out
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  7. #7
    SitePoint Zealot pacman2504's Avatar
    Join Date
    Oct 2003
    Location
    North Wales
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is the session 'active' being set?
    Yes, it is being set.
    In the land of the witless, the half-wit is king.
    Business and liability insurance quotes

  8. #8
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    and the 'active' session is only set on page 1?
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  9. #9
    SitePoint Zealot pacman2504's Avatar
    Join Date
    Oct 2003
    Location
    North Wales
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, on page 1 only. All of the other pages check to see if it is set and then relocate accordingly.
    In the land of the witless, the half-wit is king.
    Business and liability insurance quotes

  10. #10
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    try the other way of checking...
    PHP Code:
    if(!isset($_SESSION['session'])) {
     
    header("Location: page1.php"); 

    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  11. #11
    SitePoint Zealot pacman2504's Avatar
    Join Date
    Oct 2003
    Location
    North Wales
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That does work in terms of checking that the session is set, but still throws me back to page 1 when it shouldn't!
    In the land of the witless, the half-wit is king.
    Business and liability insurance quotes

  12. #12
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    post some code, specifically the session start & variables from page 1 and the session check on page 2.....
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  13. #13
    SitePoint Zealot pacman2504's Avatar
    Join Date
    Oct 2003
    Location
    North Wales
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm, I was hoping that there was something glaringly obvious that I was doing wrong and that I wouldn't have to post some code.

    I'll do a bit more testing but assuming that there are no other errors in my code, what I am trying to achieve above should work from the above examples?

    If I can't get it to work I'll post some code a bit later on.

    Thanks for your help.
    In the land of the witless, the half-wit is king.
    Business and liability insurance quotes

  14. #14
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    No problem, from what you say it should work.....?
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  15. #15
    SitePoint Zealot ceefour's Avatar
    Join Date
    Feb 2005
    Location
    Bandung, Indonesia
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pacman2504
    Ceefour,

    Perhaps I haven't explained myself well enough.

    I understand that session variables will persist for the duration of the session, but when I said that I echo all of the form variables, I meant in this manner:
    PHP Code:
    <input type="text" name="example" value="<?echo $_SESSION['example'];?>" />
    So if the user returns to this page, the value that they have entered, and which is stored as a session variable, will display as the value of that particular field.

    Is there a better way of doing this, so that the user does not have to re-input all details?
    Weird... why would you display ALL input from the previous pages in the wizard?
    I guess wizards work by displaying only the required information at the current page, any information from the previous pages are only "remembered" somewhere but not displayed.
    Of course if they want to go "Back" then you'll need to redisplay the stored values for that page (if exists), I can't see a better way. I'm not sure about browser caching mechanisms, even if you use the Back button it'll always reload pages that are "POST"-ed.


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
  •