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.

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.

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.

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.

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:

<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:


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?”.

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 :wink: )

I don’t complain about anything people do but that behavior irritates me more than anything.

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.