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... Link to comment https://forums.phpfreaks.com/topic/21311-how-to-return-to-previous-page-using-php/ 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] Link to comment https://forums.phpfreaks.com/topic/21311-how-to-return-to-previous-page-using-php/#findComment-94868 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? Link to comment https://forums.phpfreaks.com/topic/21311-how-to-return-to-previous-page-using-php/#findComment-94885 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 Link to comment https://forums.phpfreaks.com/topic/21311-how-to-return-to-previous-page-using-php/#findComment-94893 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.