[php] Go Back to Previous Page

I am using a form to "Rate" a page. This form "posts" data to a php script elsewhere. I simply want to display a link after the form is processed which will bring the user back to previous page. Can I do this using javascript in my php script?

GF

This question is related to php javascript

The answer is


We can show a back button using html code in our pages which can take the browser window to the previous page. This page will have a button or a link and by clicking it browser will return to previous page. This can be done by using html or by using JavaScript in the client side.

Here is the code of this button

<INPUT TYPE="button" VALUE="Back" onClick="history.go(-1);">

Using JavaScript

We can use JavaScript to create a link to take us back to previous or history page. Here is the code to move back the browser using client side JavaScript.

<a href = "javascript:history.back()">Back to previous page</a>

history.go(-1) this is a possible solution to the problem but it does not work in incognito mode as history is not maintained by the browser in this mode.


... By looking at Facebook code ... I found this

Go Back to Previous Page


If your web-server correctly redirects you to a new page after you made a post, the regular "back" button on the browser will work. Or the "history.go(-1)" in javascript. This will produce a filled out form.

However, if the server just returns new content without redirecting - then history.go(-1) is not going to help you. At that point you have lost your form.

If you just want to simply go back to the previous url - just link to it with an A HREF tag. That will show you an empty form.


Like this:

<?php

    if (isset($_SERVER["HTTP_REFERER"])) {
        header("Location: " . $_SERVER["HTTP_REFERER"]);
    }

?>

I think button onclick="history.back();" is one way to solve the problem. But it might not work in the following cases.

  1. If the page gets refreshed or reloaded.

  2. If the user opens the link in a new page.

To overcome these, the following code could be used if you know which page you have to return to. E.g. If you have a no of links on one page and the back button is to be used to return to that page.

<input type="button" onclick="document.location.href='filename';" value="Back" name="button" class="btn">

We can navigate to the previous page by using any of the below.

window.location.href="give url you want to go";

or

window.history.back();

or

window.history.go(-1);

or

window.history.back(-1);

You specifically asked for JS solutions, but in the event that someone visits your form with JS disabled a PHP backup is always nice:

when the form loads grab the previous page address via something like $previous = $_SERVER['HTTP_REFERER']; and then set that as a <input type="hidden" value="$previous" /> in your form. When you process your form with the script you can grab that value and stick it in the header("Location:___") or stick the address directly into a link to send them back where they came from

No JS, pretty simple, and you can structure it so that it's only handled if the client doesn't have JS enabled.


Try this:

$previous = "javascript:history.go(-1)";
if(isset($_SERVER['HTTP_REFERER'])) {
    $previous = $_SERVER['HTTP_REFERER'];
}

in html:

<a href="<?= $previous ?>">Back</a>

The JavaScript code is initialize as fallback for HTTP_REFERER variable sometimes not work.


Depends what it is that you're trying to do it with. You could use something like this:

echo "<a href=\"javascript:history.go(-1)\">GO BACK</a>";

That's the simplest option. The other poster is right about having a proper flow of history but this is an example for you.

Just edited, orig version wasn't indented and looked like nothing. ;)