views:

135

answers:

3

i m using a button to go back page (let zero.php) so i m using this

<input type="button" value="Back" class="button" onClick="javascript:history.go(-1)" />

but it fails when we do some stuff on that page

like from one.php we add some data and go to two.php( where database quary runs) and again comes to a.php and then click on that button then it goes nowhere(logically it goes two two.php but there is header location of one.php is described)

another method is

<a href="last.php"><input type="button" value="Back" class="button" /></a>

but it look very odd

please suggest me the best way to go back through <input type="button"> or

<button type="submit">
+1  A: 

You could use this piece of javascript:

<input type="button" onclick="javascript:document.location.href='last.php'">

or a simple GET-form:

<form method="GET" action="last.php"><button type="submit"></form>

I'd prefer the second over the first, since it works with javascript disabled

Cassy
@Cassy there is already a form is running
diEcho
Okay, then you can still go with the first piece
Cassy
A: 

The javascript history.back(-1) will only take you back one page in your browsing history. If you're jumping around pages as much as you described, then the javascript link won't be taking you to the page that you want because it's working much like your browsers back button.

Using a hardcoded back button much like Cassy described will work for you, and will redirect your user back to the correct page no matter where they are located within your site.

Good luck on your project.

Chris
A: 

Given that there's already a form running, you could use

<input type="submit" name="action" value="Back" class="button">

Then, when processing form input, first check to see whether $_POST["action"] == "Back". If so, go back to the previous page. The previous page may be kept in a hidden form value, or a stack within your application's session system.

eswald
this is not valid answer
diEcho
It's being used in a production system, so I fail to see why it's not valid. It does take a bit more code on the back end than other systems, but can be more robust when used properly.
eswald