How to write the code for the back button?

137,798

Solution 1

<button onclick="history.go(-1);">Back </button>

Solution 2

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>

Solution 3

<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;
}

Solution 4

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.

Solution 5

<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

Share:
137,798

Related videos on Youtube

tintincutes
Author by

tintincutes

NewBie in a programming world New to Java World :-) Just started googletalk:athens143

Updated on July 09, 2022

Comments

  • tintincutes
    tintincutes almost 2 years

    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> 
    
    • Matt Ball
      Matt Ball over 13 years
      And your problem is what, exactly?
    • EboMike
      EboMike over 13 years
      Probably the nested <a tag> inside the <input> tag. (Which should be type button, not type submit). But Dave already posted an answer.
  • tintincutes
    tintincutes over 13 years
    this one works perfect... can i adjust the space of the back button?
  • Basic
    Basic over 13 years
    Nice options - You may want to include type="button" to your button code so it validates :)
  • tintincutes
    tintincutes over 13 years
    @Basiclife: how could you do that?
  • davehauser
    davehauser over 13 years
    Is type really a mandatory attribute? w3schools.com/tags/tag_button.asp
  • Eiko
    Eiko over 13 years
    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.
  • Basic
    Basic over 13 years
    @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.
  • GSto
    GSto over 13 years
    @tintin yes, you can. and the -1 is telling it where to go in the history, in this example back one.
  • tintincutes
    tintincutes over 13 years
    i see how can you adjust the width of the button and also the spacing?
  • tintincutes
    tintincutes over 13 years
    @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
  • Eiko
    Eiko over 13 years
    No worries. See the other answers in that case.
  • Basic
    Basic over 13 years
    I believe he meant history.go(-1) :)
  • davehauser
    davehauser over 13 years
    @Basiclife - But it would surely not be amiss, so I edited my answer and added the type attribute :-)
  • DavChana
    DavChana almost 12 years
    @davehauser see w3fools for recommending w3schools.com
  • Haywire
    Haywire over 11 years
    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.
  • Eric Laoshi
    Eric Laoshi about 9 years
    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!
  • Undefitied
    Undefitied over 7 years
    don't forget type="button" attribute
  • Quentin
    Quentin about 6 years
    $_SERVER['HTTP_REFERER']; is highly unreliable.
  • pranav shinde
    pranav shinde about 6 years
    in my case window.history was not working,I tried this in codeigniter,and it gives me correct redirection
  • ToolmakerSteve
    ToolmakerSteve about 5 years
    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 <a href=" <?= $_SERVER['HTTP_REFERER'] ?> ">Back</a>.
  • ToolmakerSteve
    ToolmakerSteve about 5 years
    ... 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
    ToolmakerSteve about 5 years
    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
    ToolmakerSteve about 5 years
    Please explain under what circumstances this is preferable to using the standard, built-in ways of going back a page, seen in other answers.
  • James Baloyi
    James Baloyi about 5 years
    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
  • pranav shinde
    pranav shinde almost 5 years
    @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