23

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> 
halfer
  • 19,824
  • 17
  • 99
  • 186
tintincutes
  • 5,618
  • 25
  • 67
  • 86

7 Answers7

61
<button onclick="history.go(-1);">Back </button>
GSto
  • 41,512
  • 37
  • 133
  • 184
23

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>
davehauser
  • 5,844
  • 4
  • 30
  • 45
  • Nice options - You may want to include `type="button"` to your button code so it validates :) – Basic Sep 07 '10 at 15:04
  • @Basiclife: how could you do that? – tintincutes Sep 07 '10 at 15:05
  • Is `type` really a mandatory attribute? http://www.w3schools.com/tags/tag_button.asp – davehauser Sep 07 '10 at 15:15
  • @Dave - I was just about to link to that :) I believe "button" should be the default type but I've had instances of browsers using submit as the default - which can cause issues if you're not expecting a submit. I've since made myself define it explicitly - That said, you're right, it's not required to validate. – Basic Sep 07 '10 at 15:34
  • @Basiclife - But it would surely not be amiss, so I edited my answer and added the type attribute :-) – davehauser Sep 07 '10 at 18:48
  • @davehauser see w3fools for recommending w3schools.com – DavChana Jul 01 '12 at 13:20
  • I am adding +1 because the second solution (using $_SERVER['HTTP_REFERER']) is the only one that works for me. I am embedding a google slide presentation. Each time the user moves forward it updates the history, so using history is useless. Thanks! – Eric Laoshi Mar 20 '15 at 17:02
  • Note: Starting with 5.4, one can rely on `=` as always available, it no longer requires short open tag `` being enabled, so the php version simplifies to `Back`. – ToolmakerSteve Apr 30 '19 at 14:25
  • ... however, clicking a link to the URL supplied by HTTP_REFERER is not quite the same as `history.back()`. Specifically, the server recreates the page and sends it to the browser again. So there is a cost in server cpu, network bandwidth, and delay before user sees page, versus javascript telling browser to go back in history. – ToolmakerSteve Apr 30 '19 at 14:38
9
<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;
}
Waqar
  • 91
  • 1
  • 1
3

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.

Eiko
  • 25,601
  • 15
  • 56
  • 71
  • 1
    You asked for a "back href". And *button* can be anything you click on - if you need form elements, specifically ask for them. Maybe put a single question in your text anyway... If you downvoted, then sorry I bothered to answer. – Eiko Sep 07 '10 at 15:21
  • @no problem it's ok. opps my bad. yes i asked only for the back href. I was already imagining that it will be automatically inside the button. sorry – tintincutes Sep 07 '10 at 15:56
  • No worries. See the other answers in that case. – Eiko Sep 07 '10 at 16:07
2
<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

Chris
  • 6,568
  • 3
  • 22
  • 21
-1

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;

        }
pranav shinde
  • 1,260
  • 13
  • 11
  • `$_SERVER['HTTP_REFERER'];` is highly unreliable. – Quentin Apr 21 '18 at 10:07
  • in my case window.history was not working,I tried this in codeigniter,and it gives me correct redirection – pranav shinde Apr 21 '18 at 10:11
  • Be aware that this sends a URL to your web server. The page will be slower to load than simply doing javascript in browser to go back one page. So only use when cannot use the javascript solution for some reason. – ToolmakerSteve Apr 30 '19 at 15:03
  • @ToolmakerSteve Absolutely correct, In my case I was having multiple views which were called through AJAX. These views were called based on the workflow that is why normal javascript was not working for me – pranav shinde May 24 '19 at 12:30
-1

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>";
Mr Smith
  • 3,318
  • 9
  • 47
  • 85
James Baloyi
  • 82
  • 12
  • Please explain under what circumstances this is preferable to using the standard, built-in ways of going back a page, seen in other answers. – ToolmakerSteve Apr 30 '19 at 15:05
  • When I wrote that answer, I wasn’t too big on JS... but I won’t say it’s all wrong, because it’s easier to implement. All you have to do is place it in the href attribute, versus adding event listeners – James Baloyi May 01 '19 at 16:03