8.3.6 Capturing and Echoing HTML Form Data
8.3.6 Capturing and Echoing HTML Form Data
We now discuss a CGI program that receives data from an HTML form that is filled by a user and echoes the field names and field values back to the browser. This can be a part of a more complex program where the echoing is used to give the user feedback. We use the CGI.pm module to capture data.
We have an HTML file that shows a Web-based subscription form for an electronic mailing list. We show the code for only the HTML form here. The mailing list is called the Assam mailing list that the author of this book has managed for many years. It is a mailing list that allows individuals to subscribe by filling in a form on the Web. Individuals can also unsubscribe by filling up a related form. The HTML code is given below.
<FORM METHOD="POST"
ACTION="http://pikespeak.uccs.edu/cgi-bin/kalita/assamListFormEcho.pl">
<TABLE BORDER="0" WIDTH="75%">
<TR>
<TD HEIGHT="18" WIDTH="30%">Your Email:</TD>
<TD HEIGHT="18" width="70%">
<INPUT TYPE="TEXT" NAME="email">
</TD>
</TR>
<TR>
<TD WIDTH="30%">First Name:</TD>
<TD WIDTH="70%">
<INPUT TYPE="TEXT" NAME="firstName">
</TD>
</TR>
<TR>
<TD WIDTH="30%">Last Name:</TD>
<TD WIDTH="70%">
<INPUT TYPE="TEXT" NAME="lastName">
</TD>
</TR>
<TR>
<TD WIDTH="30%">Surface Mailing Address</TD>
<TD WIDTH="70%">
<INPUT TYPE="TEXT" NAME="surfaceAddress" MAXLENGTH="80" SIZE="80">
</TD>
</TR>
<TR>
<TD WIDTH="30%">Occupation</TD>
<TD WIDTH="70%">
<INPUT TYPE="TEXT" NAME="occupation">
</TD>
</TR>
<TR>
<TD WIDTH="30%">Telephone Number</TD>
<TD WIDTH="70%">
<INPUT TYPE="TEXT" NAME="telephoneNumber">
</TD>
</TR>
</TABLE>
<P><FONT COLOR="#DE190F">To be able to unsubscribe from the list on
a later date by coming to this site on the World Wide Web, please provide
answers to the following two questions. You can remove
yourself from the mailing list by filling in the form for
<A
HREF="http://pikespeak.uccs.edu/~kalita/unsubscribe.html">unsubscribing
from the Assam List </A> </FONT> <FONT COLOR="blue"> <BR><B>
Please remember: If you don't
fill proper values for answers to
the two questions and enter inappropriate
values, you will be removed from the mailing list by
its administrator(s).</B> </FONT> </P>
<TABLE WIDTH="75%">
<TR>
<TD WIDTH="46%">What is your mother's maiden name (her last name
before marriage):</TD>
<TD WIDTH="54%">
<INPUT TYPE="TEXT" NAME="motherMaidenName">
</TD>
</TR>
<TR>
<TD WIDTH="46%">What is your village, town or city of birth?</TD>
<TD WIDTH="54%">
<INPUT TYPE="TEXT" NAME="homeTown">
</TD>
</TR>
</TABLE>
<P>
<INPUT TYPE="submit" NAME="submit" value="Become a Subscriber">
<INPUT TYPE="reset" NAME="rest" value="Clear Form">
</P>
</FORM>
The form has several fields or input boxes. Each field has a name. The names of the fields are: email, firstName, lastName, surfaceAddress, occupation, telephoneNumber, motherMaidenName, and homeTown. The form’s input boxes are organized into two tables for a pleasant display. All but the last two form fields are in one table. The last two fields are in a table by themselves. It is a common practice to use HTML tables to format form data. HTML allows almost all tags inside the <FORM> and </FORM> tags so that the form can be made nice to look at.
The Web page as it looks on a Web browser is shown in Figure 8.9. This figure shows the form with all the input boxes filled with values. Note that the ACTION attribute of the form is given as a URL on the machine pikespeak.uccs.edu. It also uses the POST method for submitting the form data.
<FORM METHOD="POST"
ACTION="http://pikespeak.uccs.edu/cgi-bin/kalita/assamListFormEcho.pl" >
On the Red Hat Linux machine pikespeak.uccs.edu discussed in Section 8.2, this program is located at /home/kalita/public_html/cgi-bin/assamListFormEcho.pl.
Figure 8.9: HTML Form whose Contents are Echoed by a CGI Program
The CGI program that services this HTML form is given below. The program uses the CGI.pm module. It captures the data from the form fields and simply echoes the data to the browser.
Program 8.6
#!/usr/bin/perl
#assamListFormEcho.pl
use CGI qw(:standard);
use strict;
##########################
my $email = param ("email");
my $firstName = param ("firstName");
my $lastName = param ("lastName");
my $address = param ("surfaceAddress");
my $telephone = param ("telephoneNumber");
my $motherName = param ("motherMaidenName");
my $homeTown = param ("homeTown");
#######Write to browser
print "Content-type: text/html\n\n";
print <
Subscribe to Assam List
Subscribing to Assam List
This is the data you entered.
Email:
$email
First Name:
$firstName
Last Name:
$lastName
Address:
$address
Telephone Number:
$telephone
Mother's Maiden Name:
$motherName
Home Village/Town/City:
$homeTown
This is just a test form handling program that echoes what you entered
and does nothing else.
Thank you!
Assam List Administrators
BROWSER_TEXT
Like many Perl modules, the CGI.pm module has two interfaces: object-oriented and functional. In the object-oriented interface, a new CGI object is constructed and then method calls are made. In the functional interface, no object is created. The CGI.pm module exports many functions and these functions can be called from within the Perl using function calls. The CGI.pm module has a large number of functions that deal with creating HTML tags, generating HTML forms
that can be sent back to the browser in response to a submit action of a prior HTML form, and in dealing with specifics of a CGI query request. These can be imported by using the :standard keyword.
use CGI (:standard);
We also use CGI::Carp.pm and import the fatalsToBrowser function explicitly so that fatal script errors are reported to the browser, at least during the development phase of a program.
The program starts by capturing the values filled in by the user of the form. Let us look at one statement that captures a form value.
my $email = param ("email");
The param function of the CGI module takes the name of a form’s field and obtains the value sent for it by the browser. It returns a string. If we look at the HTML corresponding to the form at hand, we see the email field right at the top. The value of the field is called $email after it is captured. If no value is supplied by the user for the field, the value of $email is simply empty.
As usual, the Perl script must write to the standard output. Whatever it writes to the standard output is sent to the browser. If the CGI program writes nothing to the standard output, the browser gives an error. Therefore, just performing a task in the background is not enough for a CGI program. It must produce an output that a browser can display as well. The first thing the CGI program must write to the standard output is one or more HTTP header lines. If it prints anything else to the standard output, the browser gives an error. If there is some code before the printing of the HTTP headers to standard output, and if any of this code produces an error and if the error message is printed
to standard output, the browser produces an Internal Server Error. For example, such an error can occur if one wants to open a file for reading or writing at the outset of the CGI program and the program cannot do so for whatever reasons. A CGI program can be run by any individual in the world. Therefore, in general, CGI programs run with very little authority on a system such as Unix machine for security reasons.
The CGI program under consideration produces an HTML table. The values that the user enters in the HTML form are captured by the CGI program and are entered in this table. The table is returned as a part of the response to the Web browser.
A CGI program usually prints a lot of HTML code. It can use a sequence of print statements to do so. In this program, we use a variant of the print statement as a short cut. The text that is printed by the CGI program to the standard output is printed using only one print statement. The beginning of this print statement is repeated below.
print <<BROWSER_TEXT
Anything that follows this line is printed till the string BROWSER_TEXT is detected starting on the first column of the program. This is informally called print to here or a here document. The string BROWSER_TEXT occurs in the first column at the very end of the program. The response of the CGI program on the browser is shown in Figure 8.10.
Figure 8.10: HTML Form whose Contents are Echoed by a CGI Program
The CGI.pm module has a large number of functions that deal with the production of HTML output. In the current program, we have used CGI.pm to just to obtain values of parameters. This is the extent to which the author of the book normally uses the CGI.pm module. However, there are many additional functions that others may find useful.
