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
Solution 1:
You can use a link to invoke history.go(-1)
in Javascript, which is essentially equivalent to clicking the Back button. Ideally, however, it'd be better to just create a link back to the URL from whence the user was posted to the form - that way the proper "flow" of history is preserved and the user doesn't wonder why they have something to click "Forward" to which is actually just submitting the form again.
Solution 2:
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.
Solution 3:
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. ;)
Solution 4:
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.