SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Preventing the user to go back to the previous page

    Hi,

    In my web application I have a 5 step process to create a website. Each step is displayed on a separate page. At one step, where there is a form that asks the details about the website, I don't want the user to go back to the previous step because it will break the process. What are my options in that case? Is there a PHP way to handle this or do I have to go JavaScript way?

    Thanks for any ideas.

  2. #2
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,176
    Mentioned
    454 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by nayen View Post
    I don't want the user to go back to the previous step because it will break the process.
    Why will it break the process?

    JS is probably not a good option, as that is unreliable. I can too easily be turned off.

  3. #3
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ralph.m View Post
    Why will it break the process?

    JS is probably not a good option, as that is unreliable. I can too easily be turned off.
    Because each step is dynamically generated based on the selections made on the previous steps and for example if they go back from Step 4 to Step 3 and then to Step 2, there will be complications. I will explain my users how to use the application but I want to make sure they will not be able to go back at a certain step.

    By the way, users of this application will know that JavaScript is a requirement.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,810
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Sounds like you need to fix the way you are doing things so that it doesn't break when people go back to a prior page.

    If JavaScript is a requirement for the code to work then the simplest solution would be to do everything in just one page - that way there is no prior page to go back to. Instead of loading a new page at the end of each step use JavaScript to send the information to the server instead and then have the JavaScript rewrite the content of the existing page ready to collect the next lot of information.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    It's simple with session. You can simply use a session variable to hold what page they're on.

    You never ever use JavaScript to enforce rules unless that rule isn't a big deal if they don't abide by it. It must always be done server side with PHP.

  6. #6
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    This can be easily done even without js. In your PHP application consolidate the four scripts into one. So assuming you have the following scripts, each for a single step:

    form_step1.php
    form_step2.php
    form_step3.php
    form_step4.php


    change it so that there is only one script you submit your form to:

    form.php

    and in your form add a hidden field that will pass the step number:

    Code:
    <input type="hidden" name="step" value="1">
    The first step form will have value 1, second step value 2, etc. Then read this value when the form is displayed or submitted and based on that decide which step should be handled. You don't really have to physically consolidate all your scripts into one, you can use an intermediate script that will route code execution to the right script, for example:

    PHP Code:
    if (empty($_POST['step'])) {
      
    // the first step will have no form submission yet
      
    include "form_step1.php";

    } elseif (
    $_POST['step'] == 1) {
      include 
    "form_step2.php";  // save step1 and display step2

    } elseif ($_POST['step'] == 2) {
      include 
    "form_step3.php";  // save step2 and display step3

    } elseif ($_POST['step'] == 3) {
      include 
    "form_step4.php";  // save step3 and display step4


    In this way all your steps will be at a single URL and hitting the back button will get the user back to before all steps.

    Edit: What I stated above is wrong, the browser may remember all the steps in spite of the url being the same and use will see something like "Document Expired, do you want to submit the data again?".

  7. #7
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Lemon was... halfway there.

    On form submit;
    If $_SESSION['step'] does not exist, set it to 1; (Possible extension: If $_SESSION['step'] does not exist and $_POST['step'] != 1, display message ("Your session expired; returning you to first step"))
    If $_POST['step'] == $_SESSION['step']; process form and increment $_SESSION['step'].
    Output $_SESSION['step'] 's form. (Anywhere you see $_POST in the above post, use $_SESSION instead )
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  8. #8
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,656
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    I don't complain about anything people do but that behavior irritates me more than anything.

  9. #9
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    Lemon was... halfway there.

    On form submit;
    If $_SESSION['step'] does not exist, set it to 1; (Possible extension: If $_SESSION['step'] does not exist and $_POST['step'] != 1, display message ("Your session expired; returning you to first step"))
    If $_POST['step'] == $_SESSION['step']; process form and increment $_SESSION['step'].
    Output $_SESSION['step'] 's form. (Anywhere you see $_POST in the above post, use $_SESSION instead )
    Well, yes, you could extend it that way... But I don't really like sessions for this purpose - if a user opens the form in multiple browsers windows/tabs then things can get screwed up because session data is shared among all open documents in one browser. But I suppose this could be solved by generating a unique form ID for each started form series, the ID being generated at step 1 and passed on in a hidden field. Then you would access session data like this: $_SESSION[$formUniqueId]['step'].

    But still, when you press the back button the user will be alerted by the browser that the form data have to be resent and this may leave them perplexed. So if javascripts is a requirement then I think felgall's solution would be best - just use one page and ajax to send form data and recreate form fields for the next step.


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
  •