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> 
2
  • 1
    And your problem is what, exactly?
    – Matt Ball
    Sep 7, 2010 at 14:55
  • Probably the nested <a tag> inside the <input> tag. (Which should be type button, not type submit). But Dave already posted an answer.
    – EboMike
    Sep 7, 2010 at 14:57

7 Answers 7

61
<button onclick="history.go(-1);">Back </button>
5
  • this one works perfect... can i adjust the space of the back button? Sep 7, 2010 at 15:02
  • 1
    @tintin yes, you can. and the -1 is telling it where to go in the history, in this example back one.
    – GSto
    Sep 7, 2010 at 15:35
  • i see how can you adjust the width of the button and also the spacing? Sep 7, 2010 at 15:55
  • 6
    Caution: history.go(-1); will always take you one step back in history, even if that page is some other website you had opened earlier in the same tab of the browser.
    – Haywire
    Jan 21, 2013 at 11:47
  • don't forget type="button" attribute
    – Undefitied
    Dec 22, 2016 at 13:09
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>
9
  • Nice options - You may want to include type="button" to your button code so it validates :)
    – Basic
    Sep 7, 2010 at 15:04
  • @Basiclife: how could you do that? Sep 7, 2010 at 15:05
  • Is type really a mandatory attribute? w3schools.com/tags/tag_button.asp
    – davehauser
    Sep 7, 2010 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 7, 2010 at 15:34
  • @Basiclife - But it would surely not be amiss, so I edited my answer and added the type attribute :-)
    – davehauser
    Sep 7, 2010 at 18:48
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;
}
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.

3
  • 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 7, 2010 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 Sep 7, 2010 at 15:56
  • No worries. See the other answers in that case.
    – Eiko
    Sep 7, 2010 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

1
  • I believe he meant history.go(-1) :)
    – Basic
    Sep 7, 2010 at 17:49
-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;

        }
4
  • $_SERVER['HTTP_REFERER']; is highly unreliable.
    – Quentin
    Apr 21, 2018 at 10:07
  • in my case window.history was not working,I tried this in codeigniter,and it gives me correct redirection Apr 21, 2018 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. Apr 30, 2019 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 May 24, 2019 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>";
2
  • Please explain under what circumstances this is preferable to using the standard, built-in ways of going back a page, seen in other answers. Apr 30, 2019 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 May 1, 2019 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.