Mike Gossland’s Perl Tutorial Course for Windows

Chapter 6. The CGI Module

Introduction to GET and POST
The CGI Module, CGI.pm
Handling Forms
Handling Form Errors
Handling Fatal Errors

Handling Form Errors

Sometimes when a user is filling in a form, he or she will leave out a crucial field or provide erroneous information. It's a fact of life that it's up to you to deal with it.

The best first line of defense against blank inputs is Javascript because the server doesn't even have to get involved in the correction. But sometimes, like when a user selects a username that's already taken, the only way to know there's an error is to check the data on the server.

Once you find an error, you have to prompt the user to enter the data again. You could just put up the original form and ask them to fill in the whole thing, but you'd be very unpopular with that approach. It's much better to fill in the form with the information they already provided, and just prompt them to correct the parts that are wrong.

Substitution of provided form fields is all that's needed for this. Let's say you put up a the same form as we've seen before, but with a twist: you'll be expected to fill in your name if you didn't provide it.

Click here to bring up this new form. Try it out without putting in a name. Here's the script to handle it.

Now that you've seen how to handle the users errors, you should learn how to handle your own!

Next Page 

Search this site or the web:

Google
just this site the whole web

Questions? Feedback?

Email me your comments!