tags:

views:

71

answers:

4

hi i have a php code here and i would like to create a "back" href to get me back where I was before.

<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> 
+4  A: 

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>
Dave
Nice options - You may want to include `type="button"` to your button code so it validates :)
Basiclife
@Dave: this one works perfect as well ;-)
tintincute
@Basiclife: how could you do that?
tintincute
Is `type` really a mandatory attribute? http://www.w3schools.com/tags/tag_button.asp
Dave
@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.
Basiclife
@Basiclife - But it would surely not be amiss, so I edited my answer and added the type attribute :-)
Dave
A: 

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
@Eiko: this one works, but the word "back" is not inside a button.
tintincute
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
@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
tintincute
No worries. See the other answers in that case.
Eiko
+1 - I don't think the -1 was justified
Basiclife
A: 
<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
@Chris: this one didn't work:-(
tintincute
I believe he meant `history.go(-1)` :)
Basiclife
+1  A: 
<button onclick="history.go(-1);">Back </button>
GSto
this one works perfect... can i adjust the space of the back button?
tintincute
@GSto: what is the (-1) stands for?
tintincute
@tintin yes, you can. and the -1 is telling it where to go in the history, in this example back one.
GSto
i see how can you adjust the width of the button and also the spacing?
tintincute