Jump to content

Archived

This topic is now archived and is closed to further replies.

FinnViking

How to return to previous page using php?

Recommended Posts


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 [u]but keeps the stuff the user did write in the form fields.[/u]

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...

Share this post


Link to post
Share on other sites
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.
[code]<?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
}
?>[/code]

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites
[code]<?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
}
?>[/code]

Ray

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.