Mike's Web

Lesson 4

  1. Advanced Form Processing
  2. Handling Checkboxes
  3. Handling Radio Buttons

Advanced Form Processing

In the last lesson, you learned how to decode form data, and mail it to yourself. However, one problem with the previous script is, it doesn't allow for any error-checking, or specialized processing. You might not want to get blank forms, or you may want to require certain fields to be filled out. You might also want to write a quiz or questionnaire, and have your script take different actions depending on the answers. All of these things require some more advanced processing of the form data.

All that's required here is to know how to test conditions in Perl. Probably the main one you'll use in a form-handling script is the if-elsif condition:

if ($varname eq "somestring") {
   ...do stuff here if the condition is true
}
elsif ($varname eq "someotherstring") {
   ...do other stuff
}
else {
   ...do this if none of the other conditions are met
}

The elsif and else blocks are optional; if you are only testing whether a particular variable is true or not, you can just use a single if block.

In Perl there are different conditional test operators, depending on whether the variable you want to test is a string or a number. Here's the list of them:

NumbersStrings
$x is equal to $y$x == $y$x eq $y
$x is not equal to $y$x != $y$x ne $y
$x is greater than $y$x > $y$x gt $y
$x is greater than or equal to $y$x >= $y$x ge $y
$x is less than $y$x < $y$x lt $y
$x is less than or equal to $y$x <= $y$x le $y

Basically, if it's a string test, you use the letter operators (eq, ne, lt, etc.), and if its a numeric test, you use the symbols (==, !=, etc.). Most form data is going to be string values, so you'll probably use the letter operators most of the time.

Let's try it. Copy your form2.cgi to a new script called form3.cgi, and insert this conditional test before the open(MAIL) statement:

if ($FORM{'name'} eq "") {
   print "<h2>Error!</h2>\n";
   print "Please fill out the field for your name.<p>";
   die;
}

Now try testing it. Just push "submit" below, without filling out any of the fields:

       Your Name: 
   Email Address: 
             Age: 
  Favorite_Color: 

A blank form submission will give you an error, but if you fill out the field for your name, it will succeed.

You can extend this to test for multiple fields at the same time:

if ($FORM{'name'} eq "" || $FORM{'email'} eq "" || $FORM{'age'} eq "") {
   print "<h2>Error!</h2>\n";
   print "Please fill out the field for your name.<p>";
   die;
}

The above code will return an error if any of the name, email, or age fields are left blank. The conditions are separated by the || operator, which is an OR statement - if any of (1 or 2 or 3) is true, then the condition is met.

Handling Checkboxes

You may want to include checkboxes in your form, to allow the viewer to select one or more options. But how do you decode these inside your CGI?

If you just want to display them in your email message, you can just print them like you would any text field; each checkbox has a different name. Here's an example of the HTML form code for checkboxes:

This example lets the viewer pick as many options as they want - or none, if they prefer. While you can set the value="whatever" part of the checkbox field to any value you want, if you use integer 1's, it will mean less code is required inside the CGI. Here's an example of the Perl code that would deal with the above checkboxes:

NOTE: if you use value=1, then your Perl code can use $FORM{$x} == 1. If you mistakenly put quotes around the 1, such as value="1", then your Perl code will not work unless you change the == operator to an eq. 1 is an integer number, "1" is a string.

Here's the above code in action.

Choose the colors you like:

Handling Radio Buttons

Radio buttons differ from checkboxes in that you can have several buttons that share the same field name in the form itself - thus allowing the viewer to only select one of a series of options. To distinguish each option, the buttons themselves must have different values. For example:

This is similar to our checkboxes example, only in this case, each radio button has the same field name, but a different value. It's easiest to set the value to a relevant name of the thing being picked - in this case the values are set to the name of the color being picked. Radio buttons can be handled in Perl fairly simply:

You see here why it is best to set the value to something significant and meaningful - it lets you just print out the radio button and its value, without having to also store another list inside your cgi, to show what each button means.

Here's the above code in action.

Choose your favorite color:

You could even take the above one step further. Say you not only want to tell the viewer what color they picked, but you want to SHOW it to them also. In your perl script, you could do something like this:

The above actually sets the background color to whatever color you picked. It's using an associative list, %colors, whose key values are the same as the data value of the radio buttons in the form itself.

Here's the example:

Choose your favorite color:


Copyright © 1997 by Jacqueline D. Hamilton All Rights Reserved.


Mike's Home | Lesson 1 | Lesson 2 | Lesson 3 | Lesson 4 | Lesson 5 | Lesson 6 | Generic Lesson