[Documentation (by subject) | Documentation by type | Documentation and information]
When information is collected by a browser it is sent to a HyperText Transfer Protocol (HTTP) server specified in the HTML form, and that server starts a program, also specified in the HTML form, that can process the collected information. Such programs are known as "Common Gateway Interface" programs, or CGI scripts.
This document is available in two formats:
To understand this complex interaction, you must first understand how a client and a server work together to deliver a "normal" HTML document. This is the "canonical" Web activity; the "usual" Web function. Then you need to understand how scripts are executed in the Web environment without mediating forms. Once these two processes are clear, you have the foundation to understand the interaction bewteen HTML forms and the scripts that process the data from those forms.
For a different approach to this same topic consult its companion piece Building blocks for CGI scripts in Perl, which provides Perl code for many common CGI tasks, making script creation fairly simple.
The following diagram shows a WWW client running on a desktop
system, Computer A, interacting with two servers: An HTTP
server running on Computer B and an HTTP server running on Computer C.
The client running on Computer A gets a document, stored in a file
named docu1.html, from the HTTP server running on Computer B.
This document contains a link to another document, stored in a
file named docu2.html on Computer C.
The Uniform Resource Locator (URL) for that link might look something like:
http://ComputerC.domain/docu2.html
If the user activates that link, the client retrieves the file from
the HTTP server running on Computer C and displays it on the
monitor connected to Computer A.
The HyperText Transfer Protocol defines communication
between the client and an HTTP server. The following example
shows what an HTTP
exchange between a Lynx client and an HTTP server running
on Computer C might look like as the client fetches docu2.html.
The client sends the following text to server:
Finally, the client sends a blank line indicating it has completed
its request.
The server then responds by sending:
Things to note here:
The diagram below shows an hypertext
document on Computer B with a link to a file on
Computer C that holds the CGI program that will be executed
if a user activates the link.
This link is a "normal" http: link, but the file is stored in such
a way that the HTTP server on Computer C can tell that the file
contains a program that is to be run, rather than a document
that is to be sent to the client as usual.
When the program runs, it prepares an HTML document on the fly, and
sends that document to the client, which displays the document as it would any
other HTML document.
Such programs are sometimes called HTTP scripts
or "Common Gateway Interface" (CGI) scripts.
Note that CGI scripts may be written in scripting languages (like Perl,
TCL, etc.) or in any other programming language (like C, Pascal, Basic).
On some HTTP servers these CGI programs are stored
in a directory called cgi-bin, and so they are also
sometimes called "cgi-bin scripts."
Here is a simple AppleScript program that can be run by a MacHTTP
server when it receives a request for the file containing the script.
When it runs, this program builds an HTML document containing the
current time and returns the document to the WWW client
that requested it.
"
return header & body
The program is stored in a file named "date", in a folder
called "scripts". When a user activates a link that points
to this script, the Web client will generate an HTTP
request that might look like:
A set of tags was added to HTML to direct a WWW
client to display a form to be filled out by a user and then
forward the collected data to an HTTP server specified in the
form.
Servers were modified so that they could then
start the CGI program specified in the form and pass the collected
data to that program, which could, in turn,
prepare a response (possibly by consulting a pre-existing database)
and return a WWW document to the user.
The following diagram shows the various components of the process.
In this diagram, the Web client running on Computer A acquires
a form from some Web server running on Computer B. It displays the
form, the user enters data, and the client sends the entered information
to the HTTP server running on Computer C. There, the data is handed off
to a CGI program which prepares a document and sends it to
the client on Computer A. The client then displays that document.
You can select this link to see
what this form looks like from your browser
The MIME-type application/x-www-form-urlencoded means that the
variable name-value pairs will be encoded the same way a URL is
encoded. In particular, any special characters, including puctuation
characters, will be encoded as
The reply may be HTML, an image file, or any other kind of
document, though returning an HTML document is most
common.
The script's response to the example query is an HTML
document that lists the variable values it received. The HTML
looks like:
The following query is being sent to the Event Calendar. It's very
similar to the one generated by the simple example, but somewhat
longer due to the complexity of the event form.
The Canonical Browser-Server Interaction
During a "normal" document exchange a WWW client
(Netscape, Mosaic, Lynx, etc.) requests a document from a WWW server and
displays that document on a user display device.
If that document contains a link to another document, and
the user activates that link, the WWW client will then fetch and
display the linked document.
Executing "scripts"
An HTTP URL may identify a file that contains a program or script
rather than an HTML document. That program may be executed when
a user activates the link containing the URL.
The time is:
" -
& (current date) & "The time is:
September 15, 1994 3:15 pm Executing a Script via an HTML Form
The ability to process fill-out forms within the Web required modifications
to HTML, Web clients, and Web servers (and eventually to HTTP, as well).
HTML Tags Related to Forms Mode
The tags added to HTML to allow for HTML forms are:
Attributes: ACTION, METHOD, ENCTYPE
Attributes: NAME, TYPE, VALUE, CHECKED, SIZE, MAXLENGTH
Attributes: NAME, MULTIPLE, SIZE
Attribute: SELECTED
Attribute: NAME, ROWS, COLS
An Example Form
This section presents a simple form and shows how it can be
represented using the HTML forms facility, filled out by a user,
passed to a server, and generate a reply. The form asks for
information about using the World Wide Web.
What a Post Query Looks Like
When the form is "submitted" as filled out above, the following
information is sent to www.cc.ukans.edu by the client:
%nn
where nn
is the ASCII value for the character in hexidecimal.
What the Server Does
The server takes the incoming data and passes it to the
program post-query, which uses it to construct a file to
return to the client.
Query Results
You submitted the following name/value pairs:
Which looks like this on the Lynx user's screen:
A Custom Events Database
The KU Events database is accessed via an HTML form that looks
like this from Lynx:
Possible Input Tag Data Types
The input tag currently supports the following data types
(depending somewhat on which client you are using):
The SELECT Tag
The RADIO and CHECKBOX fields can be used to specify multiple
choice forms in which every alternative is visible as part of
the form. An alternative is to use the SELECT element which
produces a pull down list. Every alternative is specified in an
OPTION element.
If you include the MULTIPLE attribute in the <SELECT> tag, the user should be able to select more than one optional value from the list. Click here to see how multiple selects works with your browser.
Lynx will render a multiple select as a set of checkboxes, rather than as
pull-down menu.
The ROWS and COLS attributes determine the visible
dimension of the field in characters.
Click here to see
how text areas are rendered by your browser.
For example, the link to the KU events database is:
Note also that the program that generates the form and the program
that processes the form may be the same program.
For example, the event query form generated by events-form-get
contains the <form> tag:
The TEXTAREA Tag
When you need to let users enter more than one line of text,
you should use the TEXTAREA element:
Some Forms Don't Really Exist
Some forms don't really exist as HTML documents; they are
produced by programs (CGI scripts). Once they are filled out,
the information provided by the user may then be sent to another
program for processing.