CMPT165-Error Checking.docx

28 views1 pages
of 1
Error Checking
•Here's a portion of a web script that handles a form
name = form["name"].value
age = form["age"].value
•If we fill in the textboxes and click the submit button, we'll get a different webpage
•But if we don't fill in either textbox, we'll get a script error
•When a textbox (or other control) isn't filled in by the user, it isn't sent to the web script
•So, if it isn't filled in, the script doesn't think it exists
•To avoid crashing when a user doesn't fill in a textbox, we can first check to see if the textbox
•Then, we can give the user a nicer, easy-to-read error message instead of a hideous red screen
filled with indecipherable messages
-The red screen error messages are for the programmer, not the user
•To see if a textbox has been filled in, we can use the has_key() function
-This function verifies that a textbox (or other control) has been sent to the script
E.g. if form.has_key("name") == True:
print "<p>Hi, " + \
form["name"].value + " ...</p>"
print "<p>You didn't enter your name.</p>"
•If has_key() tells us that the control hasn't been sent to the form (that is, has_key() gives a
value of False), then we shouldn't try to use that control
•You can use has_key() to check for errors in other controls as well
-Radio button groups with no selection made
-Empty password boxes
-Empty text areas
-Unchecked checkboxes (this would probably not be an error)