Jump to content


Photo

How to return to previous page using php?


  • Please log in to reply
3 replies to this topic

#1 FinnViking

FinnViking
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 19 September 2006 - 06:59 PM


I have tried search here, but no luck so far, therefore I ask:

Is there a way to jump back to the previous page the user visited, using php?

I am validating user input from a html form, and want to display an error message if something is wrong, and with this error message I would like to show a link or something that not only takes the user back to that form but keeps the stuff the user did write in the form fields.

Provided that this is possible? If not, I guess I will have to live with a empty form, in which case I think I can manage by myself...



#2 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 19 September 2006 - 07:24 PM

Here is a sample script. I all references the same page but should be able to figure it out

If you want to use the POST method let me know.
<?php
if(isset($_GET['submit'])){
// check stuff here
$link = "<a href=\"".$_SERVER['PHP_SELF']."?back=yes&";
foreach($_GET as $key => $val){
  if($key <> 'submit'){
 $link .= "$key=$val&";
}
}
$link .= "\">BACK</a>";
echo $link;


} else {
    if(isset($_GET['back'])){
        $name = $_GET['name'];
        $address = $_GET['address'];
    } else {
        $name = "";
        $address = "";
    }

?>
<form method=GET action="<?=$_SERVER['PHP_SELF']?>">
Name: <input name=name type=text value="<?=$name?>"><br>
Address: <input name=address type=text value="<?=$address?>"><br>
<input name=submit type=submit value=submit>
</form>
<?php
}
?>


#3 FinnViking

FinnViking
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 19 September 2006 - 07:43 PM


Thank You, craygo, so far.

I´ll have a closer look at Your reply tomorrow, it is quite late over here now.

And method=POST is the one in use, is there a difference in what You write in that case?

#4 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 19 September 2006 - 08:04 PM

<?php
if(isset($_POST['submit'])){
// check stuff here
echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=POST>";
foreach($_POST as $key => $val){
  if($key <> 'submit'){
echo "<input type=hidden name=\"".$key."\" value=\"".$val."\">";
}
}
echo "<input type=submit name=back value=submit>";
echo "</form>";
} else {
    if(isset($_POST['back'])){
        $name = $_POST['name'];
        $address = $_POST['address'];
    } else {
        $name = "";
        $address = "";
    }

?>
<form method=POST action="<?=$_SERVER['PHP_SELF']?>">
Name: <input name=name type=text value="<?=$name?>"><br>
Address: <input name=address type=text value="<?=$address?>"><br>
<input name=submit type=submit value=submit>
</form>
<?php
}
?>

Ray




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users