Jump to content

Page Redirect after Error Message is Output to the Browser


makamo66

Recommended Posts

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?

 

 

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.

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.

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.

Archived

This topic is now archived and is closed to further replies.

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