Previous | Top | Next | Search | Comments

CGI programming

GETTING INPUT FROM FORMS
FORMs allow for user-friendly input.

In the "old days," there was the <ISINDEX> HTML tag. By inserting this tag into the HEAD of your HTML documents, a text field appeared on your page allowing you to supply input for a script. The technique still works but is limiting. The developers at NCSA created the CGI specification providing the means for getting a wider variety of input called FORMs. At the time, this new development was called HTML+.

FORMs provide multiple types of user input including:

  • check boxes
  • hidden variables
  • pop-up menus
  • radio buttons
  • scrolling lists where one or more items can be selected
  • single and multiple line text fields
Each of these input elements assign data to programer-defined variables in the FORM. When the FORM is "submitted", these variables and their contents are sent to your CGI script for processing.

Each FORM must have a begining and ending FORM tag. The FORM tag must have an ACTION attribute. The ACTION attritute is a URL pointing to your CGI script. The FORM tag optionally includes a METHOD attribute whose value is either GET, POST, or PUT. The default value is GET.

The differences between GET and PUT are subtle. FORMs using GET can send a limited amount of data to the remote script and these FORMs display their contents as a URL in your browser's location field. FORMs using POST can send much more data to the remote script and do not display their contents as URLs. The PUT method is not widely implemented by HTTP servers yet. It is used to copy files from your computer to the host.

Examples #1 and #2 (below) illustrate some of the subtle differences between GET and POST. Both examples have an ACTION attribute pointing to the previous script in the "Using Environment Variables" section. Notice the differences in output.

Examples #3 and #4 use the same GET and POST techniques as #1 and #2, but these scripts produce output that begins to be useful.

EXAMPLE #1

This FORM's ACTION attribute points to the environment variable script from the "Using Environment Variables" section and its METHOD attribute is GET.

Name?
Address?

EXAMPLE #2

This FORM's ACTION attribute points to the environment variable script from the "Using Environment Variables" section and its METHOD attribute is POST.

Name?
Address?

EXAMPLE #3

This FORM's ACTION attribute points to a script that takes the form's input and does some simple processing. It's METHOD attribute is GET.

Name?
Address?

EXAMPLE #4

This FORM's ACTION attribute does the same processing as the example above. It's METHOD attribute is POST.

Name?
Address?

THE CODE

#!/usr/local/bin/perl

# 04-gettingInput.cgi
# This script handles input via GET and PUT from forms.
# It also does some simple processing
# on the REMOTE_HOST environment variable.

# Eric Lease Morgan
# from Becoming a World Wide Web Server Expert
# http://sunsite.berkeley.edu/~emorgan/waves/
# 04/01/97 - renamed file as a .cgi
# 01/20/97 - Martin Luther King Day

# include a cgi processing library.
# consider also cgi.pm
require "cgi-lib.pl";

# extract the input from the form
# and put it into an array, @input
&ReadParse (*input);
$n = $input {'name'};
$a = $input {'address'};

# determine whether or not the user is from NCSU
$ncsu = 0;
$h = $ENV{REMOTE_HOST};
if ($h =~ /\.ncsu\./i) {$ncsu = 1}
if ($h =~ /^152\./)    {$ncsu = 1}

# create the http header
$header  = "MIME-Version: 1.0\n";
$header .= "Content-type: text/html\n";
$header .= "\n";

# start the html
$html  = "<html>\n";
$html .= "<head>\n";
$html .= "<title>\n";
$html .= "Example #4 - Getting input from FORMs\n";
$html .= "</title>\n";
$html .= "</head>\n";
$html .= "<body>\n";
$html .= "<p>You said your name was <b>$n</b> and your email was <b>$a</b>.</p>\n";
$html .= "\n";

# echo whether or not they are from NCSU
if ($ncsu) {
  $html .= "<p>According to your computer's Internet name or IP address, ";
  $html .= "you <b>are</b> a student, staff, or faculty member of NCSU.</p>\n";
  }
else {
  $html .= "<p>According to your computer's Internet name or IP address, ";
  $html .= "you <b>are not</b> a student, staff, or faculty member of NCSU.</p>\n";
  }

# finish the html
$html .= "</body>\n";
$html .= "</html>\n";

# output the header and html
print "$header$html";

# quit gracefully
exit;

HOW IT
WORKS

This example builds on the previous examples:
  1. The script is initialized and documented.
  2. A perl library (cgi-lib.pl and described below) is required.
  3. The contents of the form are parsed (&ReadParse (*input);) by the library and the results are assigned values ($n = $input {'name'}; and $a = $input {'address'};).
  4. The client's host name is determined ($h = $ENV{REMOTE_HOST};).
  5. Using regular expressions, the host name is evaluated for a specific strings (if ($h =~ /\.ncsu\./i) {$ncsu = 1} and if ($h =~ /^152\./) {$ncsu = 1}).
  6. The HTML data is initialized and built as in the previous examples.
  7. The HTTP header and HTML data are returned to the server.
  8. The script exits.

Obviously, this is script is much more complicated than the previous examples, but this is the first script that approaches real-world applicability. The most important difference between this script and the previous examples is the inclusion of the Perl library, cgi-lib.pl. This library, written by Steve Brenner, removes much of the complexity of CGI scripting by decoding and parsing the input of forms. CGI.PM, another more robust and full-featured CGI scripting library by Lincoln Stein, is an alternative to cgi-lib.pl and takes advantage of Perl5's object oriented nature. Either library is an indespensible tool for your CGI scripting needs.


Previous | Top | Next | Search | Comments

Version: 1.5
Last updated: 2004/12/23. See the release notes.
Author: Eric Lease Morgan (eric_morgan@infomotions.com)
URL: http://infomotions.com/musings/waves/