I have a php code here and I would like to create a "back" href to get me back to where I was before. Here's what I have:
<input type="submit" <a href="#" onclick="history.back();">"Back"</a>
<html>
<head></head>
<body>
<?php
// get form selection
$day = $_GET['day'];
// check value and select appropriate item
if ($day == 1) {
$special = 'Chicken in oyster sauce';
}
elseif ($day == 2) {
$special = 'French onion soup';
}
elseif ($day == 3) {
$special = 'Pork chops with mashed potatoes and green salad';
}
else {
$special = 'Fish and chips';
}
?>
<h2>Today's special is:</h2>
<?php echo $special; ?>
<input type="submit" <a href="#" onclick="history.back();">"Back"</a>
</body>
</html>
Basically my code sends data to the next page like so:
**Referring Page**
$this = $_SERVER['PHP_SELF'];
echo "<a href='next_page.php?prev=$this'>Next Page</a>";
**Page with button**
$prev = $_GET['prev'];
echo "<a href='$prev'><button id='back'>Back</button></a>";
<a href="javascript:history.back(1)">Back</a>
this one (by Eiko) is perfect, use css to make a button of <a>
...
eg you can use this css class in <a>
as `
<a class=".back_button" href="javascript:history.back(1)">Back</a>`
.back_button {
display:block;
width:100px;
height:30px;
text-align:center;
background-color:yellow;
border:1px solid #000000;
}
If you want to do it (what I think you are trying right now) then replace this line
<input type="submit" <a href="#" onclick="history.back();">"Back"</a>
with this
<button type="button" onclick="history.back();">Back</button>
If you don't want to rely on JavaScript then you could get the HTTP_REFERER
variable an then provide it in a link like this:
<a href="<?php echo $_SERVER['HTTP_REFERER'] ?>">Back</a>
In my application,above javascript function didnt work,because i had many procrosses inside one page.so following code worked for me hope it helps you guys.
function redirection()
{
<?php $send=$_SERVER['HTTP_REFERER'];?>
var redirect_to="<?php echo $send;?>";
window.location = redirect_to;
}
<input type="submit" <a href="#" onclick="history.back();">"Back"</a>
Is invalid HTML due to the unclosed input
element.
<a href="#" onclick="history.back(1);">"Back"</a>
is enough
You need to tell the browser you are using javascript:
<a href="javascript:history.back(1)">Back</a>
Also, your input element seems out of place in your code.
Source: Stackoverflow.com