dckantor Posted August 15, 2008 Share Posted August 15, 2008 How can I direct the user back to the top of the page if they have not filled in required fields in a form? Thanks Link to comment https://forums.phpfreaks.com/topic/119862-go-to-top-of-page/ Share on other sites More sharing options...
knowj Posted August 15, 2008 Share Posted August 15, 2008 you could use this method: if ($error) { header("Location: myform.php#top") exit(); } you would however loose your POST results. A more effective method would to be to have your form action as <form action="<?=$_SERVER['PHP_SELF']?>#top" method="post"> With your PHP directing the user to the correct page if the form is valid. you would need an anchor point with the id="top" for both of these examples. The 2nd example will allow keep your post data. Both examples will require you to process the form within the same file as HTML of the form. Link to comment https://forums.phpfreaks.com/topic/119862-go-to-top-of-page/#findComment-617533 Share on other sites More sharing options...
corbin Posted August 15, 2008 Share Posted August 15, 2008 you could use this method: if ($error) { header("Location: myform.php#top") exit(); } you would however loose your POST results. A more effective method would to be to have your form action as <form action="<?=$_SERVER['PHP_SELF']?>#top" method="post"> With your PHP directing the user to the correct page if the form is valid. you would need an anchor point with the id="top" for both of these examples. The 2nd example will allow keep your post data. Both examples will require you to process the form within the same file as HTML of the form. I could be wrong (as I often am), but I'm fairly sure that # anchors are handled entirely client side, and that header command could do weird things. Guess I need to test it (or have you already?). Link to comment https://forums.phpfreaks.com/topic/119862-go-to-top-of-page/#findComment-617660 Share on other sites More sharing options...
knowj Posted August 15, 2008 Share Posted August 15, 2008 All that will happen is that the browser will realise that an anchor has been set and it will jump to whereever the anchor is. I know IE (6 at least. yes around 20% of the web still use it) automatically jumps to the top of the page after any http request where as FF, safari and opera will try to keep the location on the page. Also with header(Location: blah.html#hello); the browser still handles a http request to the content and works off the given url. there may be a browser out there that won't like the 1st option. Personally i would only use the 2nd option as this allows you to retain the data the user entered by echoing the post data into the relevant inputs. Link to comment https://forums.phpfreaks.com/topic/119862-go-to-top-of-page/#findComment-617696 Share on other sites More sharing options...
corbin Posted August 15, 2008 Share Posted August 15, 2008 I wasn't quite sure, because if the browser didn't parse the # anchor out after the Location header, then it would request the filename including the anchor from the server. Link to comment https://forums.phpfreaks.com/topic/119862-go-to-top-of-page/#findComment-617705 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.