replicating browser back button.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

replicating browser back button.

Post by IGGt »

Is there a way that I can replicate the action of pressing the browsers back button in html/php (or javascript if necessary), in order to return to a previously executed php page, without having to re-execute the php. What I have is:

page1.html : contains a form the user inputs details into. this loads the data into:

page2.php : this runs a query against the input data, and displays the results. Each line of the results has a link to:

page3.php : this displays further information on each item. From here I want to be able to go back to page2.php, but without re-running the query (this works fine when using the browsers back button).

Is there a way to achieve this using html/php/javascript.

someone suggested:

Code: Select all

<script>
	function backhref(a) {
	if (document.referer == a.href) {
	history.back();
	return false
	}}
</script>

Code: Select all

<p><a href="page2.php" onclick="return backhref(this)">back</a></p>
But this didn't work, it tried to re-run the PHP, and without the input from page1.html, it didn't work.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: replicating browser back button.

Post by Jonah Bron »

I've never played with the cache, but it looks like you can put this into your code to tell the browser to cache the page:

Code: Select all

header('Cache-Control: max-age=4000, public')
And, if you can, use GET instead of POST for the form in page1.php.

http://www.mnot.net/cache_docs/
lavaeagle
Forum Newbie
Posts: 21
Joined: Thu Sep 09, 2010 1:41 pm

Re: replicating browser back button.

Post by lavaeagle »

The challenge here is that because PHP is server side it goes through the server then back to the client. What your looking for I deal with at my work every single day, but if you do it like this your results become dynamic and start to take care of themselves :)

page1.php

Code: Select all

<input type="text" name="item1" value="<?php if(isset($_GET['item']){ echo $_GET['item']; } ?> />
page2.php

Code: Select all

<?php 

$item = isset($_POST['item'])? $_POST['item'] : $_GET['item'];
$ite1 = mysql_query("SELECT * FROM items WHERE item='$item'"); 
$ite1_fa = mysql_fetch_assoc($ite1);

Results: <a href="page.php?item=<?php echo $item; ?>"><?php echo $ite1_fa['item_title_or_something_else_of_this_nature_foo'] ?></a>

?>
page3.php

Code: Select all

<a href="page.php?item=<?php echo $item; ?>">Back to Results!</a>
Let me know if I can clarify for anything for you!
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: replicating browser back button.

Post by Jonah Bron »

@lavaeagle I think he has that part down, the issue is he doesn't want the server load from people just going back to the page.
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Re: replicating browser back button.

Post by IGGt »

Cheers,

that has been some help, mainly it answered a question I was wondering about which is how to get from page2 to page3 ( I have used the process you listed for getting from page3 to page2).

I'm not entirely sure whether it will work for what I am after though.

at present:

Page1: I simply use a form with two variables added for start and end times. I use POST to get the data from the form to page2.

page2: this runs the query against the database using the start and end dates from page1. It then displays the results in a table (two columns, a jobid, and the startdate). as the table is created using a 'While(...) loop, I can create a variable to hold the jobname as part of each loop. I then append this variable to the <a href =.... part of the table, meaning that each entry now has a link to page3 similar to "page3.php?jobid=1234"

page3: I use $_GET to read in the jobid, which I then use as part of another query to show the details from that particular job.


Alas, I don't quite see how to get back from page3 to page2 without re-running the query.
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Re: replicating browser back button.

Post by IGGt »

I finally found an answer,and I can't believe how simple it is.

Code: Select all

<a href="javascript:history.go(-1)">Go Back</a>
thats it. works like a dream.
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Re: replicating browser back button.

Post by IGGt »

A recent discovery I have made in relation to this, is that it doesn't work if you set $_SESSION variables, in this instance it will then ask you to confirm the action (this applies equally to the javascript and the browser back button).
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: replicating browser back button.

Post by Weirdan »

IGGt wrote:A recent discovery I have made in relation to this, is that it doesn't work if you set $_SESSION variables
It works if you adjust session_cache_limiter(). By default it's set to disallow caching pages where session is used - thus the behaviour you observe.
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Re: replicating browser back button.

Post by IGGt »

That's brilliant. cheers
Post Reply