Jump to content

How to return to previous page using php?


FinnViking

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

Link to comment
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]
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.