CGI and Perl

HTML FormsThe Foundation of an Interactive Web

  • Generating HTML On-the-Fly with Perl5
  • Heady Stuff--Generating More Advanced
  • (Possibly Dynamic) HTML

This chapter serves as a review of HTML forms. I will assume you have a working knowledge of HTML tags. The HTML tags discussed in this review are limited to those that pertain to creating forms. Later in this chapter, I will discuss some more advanced HTML tags to give your forms a better look and feel. This chapter introduces the CGI modules available for use with Perl5 and how they can be used to simplify the generation of HTML forms.

The CGI modules consist of several packages, or classes, written by various Perl contributors. These modules contain some very useful abstractions for HTML generation and CGI processing. Instead of learning how to parse the received data within your script, you can leverage the work already done by these Perl experts. The methods provided by these classes can take care of the dirty work and give you more flexibility to be creative with your Web content. As you will see, the CGI::Form package gives you an easy-to-use programming interface to create HTML forms.

As you must already know, HTML forms are an essential reason for the Web's popularity. Forms bring a powerful interactive aspect to the Web. Another thing the HTML form provides is a cross-platform user interface. This capability is much more subtle than the first, but if deciding on a client platform is a difficult decision for you, HTML forms may be the answer. If you want to write simple applications such as the ones in subsequent chapters (or even more complex ones), you can focus on one piece of code to run on your server and provide a platform-independent user interface to your application through the Web. Forms still have some drawbacks, and these are mentioned in this chapter but discussed in greater detail in subsequent chapters.

HTML forms essentially consist of three things: a form declaration or header, one or more input fields, such as text fields or list boxes, and the button that submits the form data. This section discusses the different field types that can make up a form. Along with each field type is an example of how the HTML code might look, as well as how you can construct the field using the CGI::Form package.

The first thing you will need to do is construct a new CGI::Form object. This can be done by using the new() method. For example:

use CGI::Form;
 $q = new CGI::Form;

The $q variable will then contain an instance of a CGI::Form object that can then be used to emit the HTML for your form using the methods listed in the following.