Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Perl CGl Programming: No experience required.
(Publisher: Sybex, Inc.)
Author(s): Erik Strom
ISBN: 0782121578
Publication Date: 11/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


The next statement, else, goes hand-in-hand with if. It forms kind of an “otherwise” clause for the if statement, saying “execute my code if the statement(s) above is false.”

A variation of else combines the two conditional statements: elseif. A full conditional block of code using all three statements looks like this:

   if (This is true)
        {
        do this;
        }
   elseif (That is true)
        {
        do that;
        }
   else
        {
        do some other thing;
        }

You can string as many of the tests together as you like. The conditionals are processed sequentially until one of them is true, then the rest of the full block is ignored.

Another noteworthy aspect of this if-else block is the way the names are printed. Notice that, because of the way the descriptive indexes were set at the top of the program, you can get to the last name with $Value[$LastName] and so forth. It gets a tad more complicated with the other values, however, because the form shipped numbers to you to denote the radio buttons; you use the numbers to pick a string from the arrays you made in the beginning of the program.

So, if $Value[$Sex] holds the number 1, you can say $Gender[$Value[$Sex]] to get $Gender[1], which is the string “female.” Likewise, if $Value[$FaveFilm] holds the number 0, you can easily use that to get element 0 from your @Film array: $Film[$Value[$FaveFilm]] = $Film[0] = “Citizen Kane”.


TIP:  Notice that the print statement that does the favorite film category has \" in it. These escaped quotes will print as quotes around the film title, rather than prematurely ending the string to the vocal consternation of the Perl interpreter.

A last code block to consider in quiz.pl is this one:

      print "<H3>You";

      if (!$OldStones)
           {
           print " don't";
           }

      print " think the Rolling Stones are too old</H3>\n";

Remember that the $OldStones flag has been set or cleared depending on whether the Rolling Stones check box sent a value to the program or not. The exclamation point (!) in Perl is a logical NOT operator; in other words, the statement says “if NOT $OldStones…” The value of $OldStones will be either 1 or 0, true or false, which are also the only possible results of the test made in an if statement. If $OldStones is 0, then NOT $OldStones would be its logical opposite, or true! The test succeeds; the “don’t” is printed.

Get it?

Processing Forms in the Real World

You’ll probably encounter very few situations on your Web site where simply printing back the information a visitor has entered—no matter how prettily —is the only thing you’ll want to do with it.

In a real poll, you would want to store the data somewhere, probably in a file, adding the data from other visitors to it as they connected with your site and filled out the form. Then, you could pull up all of the data and process the results. quiz.pl and its HTML counterpart could be used in a survey of favorite films, for example, with very little modification. You wouldn’t need the first- and last-name information at all.

This is the kind of thing that keeps Web visitors coming back to your site.

Remember once again: You are limited only by your imagination. If you can think of it, chances are it can be done.

The Polling Booth Web site, described at the beginning of this skill, keeps track of the scores of visitors who take the “Problem Solving” IQ test. The scores are collated and can be displayed in a variety of ways.

Take a look at this Web site again for some ideas on how you might display the results of your own quizzes. You could show the number of correct answers versus incorrect answers; you could calculate the percentage of correct answers and show the results graphically, with a bar or some other artifice.

Again, if you feel insecure about your HTML skills, you can download the source code of the Polling Booth Web site pages with your Web browser. This code will show you how to set up the various fields of information on the Polling Booth’s pages; you can easily tailor the code to fit your own requirements.


EXERCISE:  The Sambar Server: Building Your Web Site

This skill provides you with the nuts and bolts of a polling section for your Sambar Web site. With a little imagination, you can expand the nuts and bolts into something that will be quite useful for you. Your visitors will enjoy it, too, if you structure it with just the right touch.

You will learn about structuring and storing form data in a disk file in Skill 7. If you want to be able to collate information gathered from your quiz forms, you’ll need to be able to store it somewhere. Meanwhile, you can put the bulk of your polling page together now:

  Think up a subject—or several—on which to quiz your visitors.
  Design and implement HTML forms to gather the information.
  Expand on the Perl/CGI concepts we’ve covered in this skill to make scripts that can get the information and store it in local variables.

Moving On

In the next skill, you’ll learn how to stuff information from an HTML form into a Perl data structure and write it to a disk file. You’ll design and implement a guest book for your Web site that visitors can both sign and display.

You’ll also learn the basics of Web security as you build your guest book program to ensure that harmful data can’t be entered into it.

Are You Experienced?

Now you can…

  build an advanced HTML quiz form, using radio buttons, check boxes, and text fields.
  intelligently process the information in the form through a Perl program
  receive information sent to your program with the POST method
  add the else and elseif statements to your repertoire of conditional expressions, in addition to the logical AND, OR, and NOT operators


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.