CGI and Perl

Other Form Elements

You can add other types of elements to your forms. The full list of available form fields is available in the HTML 3.2 evolving specification, which can currently be found at

Generating HTML On-the-Fly with Perl5

There are two ways to define your form. The first is to create a static HTML file that contains all your form field definitions and reference the form to a particular CGI script. Another, perhaps easier, method is to dynamically display your form within your CGI script. The script can be called with a method get or a method post. When it is called with method get, you can respond with the form itself, and when it is called with method post, you can process the form request. Everything you print to stdout from your CGI script is returned back to the client (browser). This makes it very easy to generate HTML on-the-fly with Perl--especially when you use the CGI modules for form generation and processing. There is one drawback to this approach in that the Web server must execute a program to respond to the request rather than simply read a potentially cached file. If you don't mind the overhead of the extra process invocation, it will definitely make the job of maintaining your form and CGI program a lot easier.

Constructing Headers

The first part of an HTML form that you should generate is the header, which contains some important information about the text that is to follow. The most important piece of information is the MIME type. It is very important that you specify the correct MIME type for your HTML data. The first line of your output should always be Content-Type: text/html;. This line tells the browser what kind of data to expect. Fortunately, by using CGI::Form, you need not worry about this detail because it is the default type. Simply calling the method header returns the correct MIME type for you.

In addition to the document header, you will also need to correctly specify information about the types of any forms that are included in the document. This section will describe the two different types of forms as well.

print $q->header();

The HTML Header The next step is to construct the HTML header. The HTML header usually consists of just a title but may also include things that go into the <BODY> tag, such as background and foreground colors. Using the start_html method in CGI::Form provides a nice way to incorporate all of your HTML header information. In this example, I am starting an HTML document with a nice lavender background color (specified as a hexadecimal RGB triplet).

print $q->start_html(-title=>"Customer Survey Page",-author=>'',
                     -BGCOLOR=> "DDDDFF");

Not included in this function, however, is the new HTML 3.2 <!DOCTYPE> directive. This directive is what all HTML 3.2 conforming documents should begin with. This directive is actually part of SGML, but it is used to identify the HTML document as such. More information about HTML and SGML can be found at Hopefully, the start_html method will incorporate this standard at some point. Types of Forms Two different MIME types are currently defined for HTML forms. The first is application/ x-www-form-urlencoded. This is the original type defined for simple forms. The second is multipart/form-data. This type was introduced by Netscape 2.0 for the transfer of large chunks of data. It is important that you choose your form type appropriately. Generally speaking, if you need to support many different types of browsers, some of which may not handle multipart/ form-data, choose the original MIME type. However, if you want to use such fields as the file upload field, you must choose multipart/form-data. Eventually, all browsers should support multipart/form-data, at which point, it would be the obvious choice.

There are two methods within CGI::Form for starting your form--startform() and start_multipart_form(). You should use whichever one corresponds to the type of form you are creating. These methods take the optional parameters $method, $request, and $encoding. You shouldn't need to worry about the $encoding parameter because the method you choose will specify the appropriate encoding mechanism for your form. $method defaults to POST, and $action defaults to the current CGI script, which is generally what you want.

by BrainBellupdated