makamo66 Posted May 19, 2010 Share Posted May 19, 2010 I know that you aren't supposed to output anything to the browser before using the header location function but what if you need to show an error message? If there is an error, for example, an invalid email address, then I want to show an error message and go back to the form submission page. After all of the errors have been corrected, I want to redirect to the next page. However since I already sent an error message, I can't send a header. Is there a way to do this without resorting to javascript? Quote Link to comment https://forums.phpfreaks.com/topic/202323-page-redirect-after-error-message-is-output-to-the-browser/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 19, 2010 Share Posted May 19, 2010 The best solution is to put your form processing code and your form on the same page. You can output any errors when the form is displayed and repopulate the form fields with the values that were already entered. Quote Link to comment https://forums.phpfreaks.com/topic/202323-page-redirect-after-error-message-is-output-to-the-browser/#findComment-1060885 Share on other sites More sharing options...
smarble53 Posted May 20, 2010 Share Posted May 20, 2010 You could try validating with javascript. Tizag.com has a great tutorial on this at http://www.tizag.com/javascriptT/javascriptform.php . it just takes the values, validates them, and displays an alert and will not submit. if there are no errors, the form will submit. I have grown to love this script, as it is quick and easy to use, even for someone who does not know a heck of a lot about javascript. Quote Link to comment https://forums.phpfreaks.com/topic/202323-page-redirect-after-error-message-is-output-to-the-browser/#findComment-1061182 Share on other sites More sharing options...
DavidAM Posted May 20, 2010 Share Posted May 20, 2010 As PFMaBiSmAd said, the best approach is the have the form and the process in the same file (usually). Users will get tired of having to retype everything they put in the form just because one field has a typo. But if I absolutely have to display a message and then redirect, I use the META REFRESH tag. <meta http-equiv="refresh" content="2;url=http://domain.com/destination.php"> It goes in the HEAD of the HTML document. The "2" in the content attribute is the number of seconds to wait before loading the page specified in the "url=" portion of the attribute. This gives the user time to read the message. <HTML> <HEAD> <META http-equiv="refresh" content="2;url=http://webdesign.about.com"> </HEAD> <BODY> <P>That was a very bad thing to do ...</P> </BODY> </HTML> Using Javascript to PRE-validate the form is OK, but the user can turn off Javascript, so you HAVE to validate on the SERVER as well. Of course, since the META REFRESH is done by the browser, I suppose it is possible for this feature to be disabled as well. So you should also have a link in the page that the user can click to go to the page you want them to go to. Quote Link to comment https://forums.phpfreaks.com/topic/202323-page-redirect-after-error-message-is-output-to-the-browser/#findComment-1061187 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.