FinnViking Posted September 19, 2006 Share Posted September 19, 2006 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... Quote Link to comment Share on other sites More sharing options...
craygo Posted September 19, 2006 Share Posted September 19, 2006 Here is a sample script. I all references the same page but should be able to figure it outIf you want to use the POST method let me know.[code]<?phpif(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] Quote Link to comment Share on other sites More sharing options...
FinnViking Posted September 19, 2006 Author Share Posted September 19, 2006 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? Quote Link to comment Share on other sites More sharing options...
craygo Posted September 19, 2006 Share Posted September 19, 2006 [code]<?phpif(isset($_POST['submit'])){// check stuff hereecho "<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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.