CGI Scripts
CGI and Forms
CGI Caveats |
Language Choice |
CGI and Forms
Let's now take a look at a very common use of CGI scripts,
processing user input supplied in forms. We'll consider
examples of both C and Perl programs applied to the problem.
A Very Simple Example
A typical use of gateway scripts is for processing information
submitted on a Web page form. For instance, the following
text entry area is a very simple example of a form. To test
it, enter some text in the box and then click on the button
named "Submit Query". You should then see another page that
echoes the information you entered. (Return to this page
using the "Back" function of the browser.)
Note that we have two things working together in this
example. First, text is entered in the form; second, the
text is passed on to a program that "processes" this information.
In the present case the processing is rather trivial: the
information is simply echoed back to the client. But the
CGI script can be designed to do much more than this with
the information; we'll look at such an example in a little
while.
The FORM Tag
The form itself is just HTML (albeit using some HTML commands
that we haven't discussed before now). The form is encompassed
in a FORM tag and there are additional commands for producing
individual elements in the form (e.g., text entry fields,
buttons, etc.). Here is the code that produced the above
simple form:
<FORM METHOD="POST" ACTION=
"http://csep10.phys.utk.edu/cgi-bin/post-query">
Please enter some text here:
<INPUT NAME="entry">
To submit the query, press this button:
<INPUT TYPE="submit"
VALUE="Submit Query">.
</FORM>
|
The program that processes the information entered into
this form, however, is not HTML. In this case, the
processing is done by a compiled C program named
post-query that resides in the cgi-bin
subdirectory of the directory holding the server software
on a computer with IP address csep10.phys.utk.edu.
Note that the form uses the value assigned to "ACTION="
in the tag to tell the server what program it should use
to process the information that is being sent from the form.
More Information on
Constructing Forms
If you're interested in learning more about constructing
HTML forms, you might start by consulting this
forms tutorial, and this set of links.
The material there will tell you more about text entry areas,
and show you how to use other form elements (checkboxes,
radio buttons, scrolling lists, etc.). The former document
includes numerous examples which you can test on the spot.
Note that as long as you are only interested in designing
forms (and not in customizing the ways in which you process
the data entered in the forms) you can use these examples
as models for creating your own working forms, and you can
test your creations with the post-query program
on either csep10.phys.utk.edu or at hoohoo.ncsa.uiuc.edu
(which processes the example forms at the NCSA site).
More Complex Examples
An example of a more complicated form (and more complicated
processing of form data) is an online science
quiz. A similar example at the same site features a
form that provides a quiz
in astronomy. If you use your browser to view the document
source for one of the science quiz question pages (i.e.,
one of the pages in the quiz that asks, not answers, a question),
you'll note the line:
<form method="post" action="/cgi-bin/quizforms/galileo/qa.pl">
From this we see that the information entered into this
form is processed by a program called qa.pl (on
the same machine as the form, which is why you don't see
an http-type url assigned to action= in this case).
This program is a Perl script that has been written
specifically to handle the information submitted by this
one form. Rather than simply echoing entered text as
post-query does, qa.pl examines the submitted
information, extracts the part that corresponds to the answer
to the question, compares this to the correct answer for
the question (which is stored in a database on the server),
and then presents the user with one of various possible
pages depending on whether or not the correct answer to
the question was entered. This illustrates that a gateway
script can do much more than simply echo arbitrary text.
If you are interested, here is a listing of the Perl
program qa.pl.
References
For additional information about CGI, see this Programming
References page.
|