📄 ch20.htm
字号:
<HTML>
<HEAD>
<TITLE>Chapter 20 -- Form Processing</TITLE>
<META>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT SIZE=6 COLOR=#FF0000>Chapter 20</FONT></H1>
<H1><FONT SIZE=6 COLOR=#FF0000>Form Processing</FONT></H1>
<HR>
<P>
<CENTER><B><FONT SIZE=5>CONTENTS</FONT></B></CENTER>
<UL>
<LI><A HREF="#ABriefOverviewofHTML">
A Brief Overview of HTML</A>
<LI><A HREF="#ServerSideINCludes">
Server-Side INCludes</A>
<LI><A HREF="#HTMLForms">
HTML Forms</A>
<LI><A HREF="#HandlingFormInformation">
Handling Form Information</A>
<LI><A HREF="#FillinginaFormandMailingtheInformation">
Filling in a Form and Mailing the Information</A>
<LI><A HREF="#DebuggingFormProcessingCGIScripts">
Debugging Form Processing CGI Scripts</A>
<LI><A HREF="#CreatingaGuestbookforYourSite">
Creating a Guestbook for Your Site</A>
<UL>
<LI><A HREF="#TheBasicGuestbook">
The Basic Guestbook</A>
</UL>
<LI><A HREF="#Summary">
Summary</A>
<LI><A HREF="#ReviewQuestions">
Review Questions</A>
<LI><A HREF="#ReviewExercises">
Review Exercises</A>
</UL>
<HR>
<P>
One of the most popular uses for CGI programs is to process information
from HTML forms. This chapter gives you an extremely brief overview
of HTML and Forms. Next you see how the form information is sent
to CGI programs. After being introduced to form processing, a
Guest book application is developed.
<H2><A NAME="ABriefOverviewofHTML"><FONT SIZE=5 COLOR=#FF0000>
A Brief Overview of HTML</FONT></A></H2>
<P>
HTML, or <I>Hypertext</I> <I>Markup</I> <I>Language</I>, is used
by web programmers to describe the contents of a web page. It
is not a programming language. You simply use HTML to indicate
what a certain chunk of text is-such as a paragraph, a heading
or specially formatted text. All HTML directives are specified
using matched sets of angle brackets and are usually called <I>tags</I>.
For example <TT><B></TT> means
that the following text should be displayed in <B>bold</B>. To
stop the bold text, use the <TT></B></TT>
directive. Most HTML directives come in pairs and surround the
affected text.
<P>
HTML documents need to have certain tags in order for them to
be considered "correct". The <TT><HEAD>..</HEAD></TT>
set of tags surround the header information for each document.
Inside the header, you can specify a document title with the <TT><TITLE>..</TITLE></TT>
tags.<BR>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Tip</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
HTML tags are case-insensitive. For example, <TT><TITLE></TT> is the same as <TT><title></TT>. However, using all upper case letters in the HTML tags make HTML documents easier to understand because you can pick out the tags more readily.
</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
After the document header, you need to have a set of <TT><BODY>..</BODY></TT>
tags. Inside the document's body, you specify text headings by
using a set of <TT><H1>..</H1></TT>
tags. Changing the number after the H changes the heading level.
For example, <TT><H1></TT> is
the first level. <TT><H2></TT>
is the second level, and so on.
<P>
You can use the <TT><P></TT>
tag to indicate paragraph endings or use the <TT><BR></TT>
to indicate a line break. The <TT><B>..</B></TT>
and <TT><I>..</I></TT>
tags are used to indicate bold and italic text.
<P>
The text and tags of the entire HTML document must be surrounded
by a set of <TT><HTML>..</HTML></TT>
tags. For example:
<BLOCKQUOTE>
<PRE>
<HTML>
<HEAD><TITLE>This is the Title</TITLE></HEAD>
<BODY>
<H1>This is a level one header</H1>
This is the first paragraph.
<P>This is the second paragraph and it has <I>italic</I> text.
<H2>This is a level two header</H2>
This is the third paragraph and it has <B>bold</B> text.
</BODY>
</HTML>
</PRE>
</BLOCKQUOTE>
<P>
Most of the time, you will be inserting or modifying text inside
the <TT><BODY>..</BODY></TT>
tags.
<P>
That's enough about generic HTML. The next section discusses Server-Side
INCludes. Today, Server-Side INCludes are replacing some basic
CGI programs, so it is important to know about them.
<H2><A NAME="ServerSideINCludes"><FONT SIZE=5 COLOR=#FF0000>
Server-Side INCludes</FONT></A></H2>
<P>
One of the newest features that has been added to web servers
is that of Server-Side INCludes or SSI. SSI is a set of fuNCtions
built into web servers that give HTML developers the ability to
insert data into HTML documents using special directives. This
means that you can have dynamic documents without needing to create
full CGI programs.
<P>
The inserted information can take the form of a local file or
a file refereNCed by a URL. You can also iNClude information from
a limited set of variables-similar to environmental variables.
Finally, you can execute programs that can insert text into the
document.<BR>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Note</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
The only real differeNCe between CGI programs and SSI programs is that CGI programs must output an HTTP header as their first line of output. See "HTTP Headers" in <A HREF="ch19.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch19.htm" >Chapter 19</A>, "What Is CGI?," for more
information.</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
Most Web servers need the file extension to be changed from <TT>html</TT>
to <TT>shtml</TT> in order for the
server to know that it needs to look for Server-Side directives.
The file extension is dependent on server configuration, but shtml
is a common choice.
<P>
All SSI directives look like HTML comments within a document.
This way, the SSI directives will simply be ignored on Web servers
that do not support them.
<P>
Table 20.1 shows a partial list of SSI directives supported by
the <TT>webSite</TT> server from O'Reilly.
Not all Web servers will support all of the directives in the
table. You need to check the documentation of your web server
to determine what directives it will support.<BR>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Note</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
Table 20.1 shows complete examples of SSI directives. You need to modify the examples so that they work for your Web site.</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
<CENTER><B>Table 20.1 A Partial List of SSI Directives</B></CENTER>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD WIDTH=283><I>Directive</I></TD><TD WIDTH=307><I>Description</I>
</TD></TR>
<TR><TD WIDTH=283><TT><!--#config timefmt="%c"--></TT>
</TD><TD WIDTH=307>Changes the format used to display dates.</TD>
</TR>
<TR><TD WIDTH=283><TT><!--#config sizefmt="%d bytes"--></TT>
</TD><TD WIDTH=307>Changes the format used to display file sizes. You may also be able to specify <TT>bytes</TT> (to display file sizes with commas) or <TT>abbrev</TT> (to display the file sizes in kilobytes or megabytes).
</TD></TR>
<TR><TD WIDTH=283><TT><!--#config errmsg="##ERROR!##"--></TT>
</TD><TD WIDTH=307>Changes the format used to display error messages caused by wayward SSI directives. Error messages are also sent to the server's error log.
</TD></TR>
<TR><TD WIDTH=283><TT><!--#echo var=?--></TT>
</TD><TD WIDTH=307>Displays the value of the variable specified by ?. Several of the possible variables are mentioned in this table.
</TD></TR>
<TR><TD WIDTH=283><TT><!--#echo var="DOCUMENT_NAME"--></TT>
</TD><TD WIDTH=307>Displays the full path and filename of the current document.
</TD></TR>
<TR><TD WIDTH=283><TT><!--#echo var="DOCUMENT_URI"--></TT>
</TD><TD WIDTH=307>Displays the virtual path and filename of the current document.
</TD></TR>
<TR><TD WIDTH=283><TT><!--#echo var="LAST_MODIFIED"--></TT>
</TD><TD WIDTH=307>Displays the last time the file was modified. It will use this format for display: <TT>05/31/96 16:45:40</TT>.
</TD></TR>
<TR><TD WIDTH=283><TT><!--#echo var="DATE_LOCAL"--></TT>
</TD><TD WIDTH=307>Displays the date and time using the local time zone.
</TD></TR>
<TR><TD WIDTH=283><TT><!--#echo var="DATE_GMT"--></TT>
</TD><TD WIDTH=307>Displays the date and time using GMT.</TD>
</TR>
<TR><TD WIDTH=283><TT><!--#exec cgi="/cgi-bin/ssi.exe"--></TT>
</TD><TD WIDTH=307>Executes a specified CGI program. It must be activated to be used. You can also use a <TT>cmd=</TT> option to execute shell commands.
</TD></TR>
<TR><TD WIDTH=283><TT><!--#flastmod virtual="/docs/demo/ssi.txt"--></TT>
</TD><TD WIDTH=307>Displays the last modification date of the specified file given a virtual path.
</TD></TR>
<TR><TD WIDTH=283><TT><!--#flastmod file="ssi.txt"--></TT>
</TD><TD WIDTH=307>Displays the last modification date of the specified file given a relative path.
</TD></TR>
<TR><TD WIDTH=283><TT><!--#fsize virtual="/docs/demo/ssi.txt"--></TT>
</TD><TD WIDTH=307>Displays the size of the specified file given a virtual path.
</TD></TR>
<TR><TD WIDTH=283><TT><!--#fsize file="ssi.txt"--></TT>
</TD><TD WIDTH=307>Displays the size of the specified file given a relative path.
</TD></TR>
<TR><TD WIDTH=283><TT><!--#iNClude virtual="/docs/demo/ssi.txt"--></TT>
</TD><TD WIDTH=307>Displays a file given a virtual path.</TD>
</TR>
<TR><TD WIDTH=283><TT><!--#iNClude file="ssi.txt"--></TT>
</TD><TD WIDTH=307>Displays a file given a relative path. The relative path can't start with the <TT>../</TT> character sequeNCe or the <TT>/</TT> character to avoid security risks.
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
SSI provides a fairly rich set of features to the programmer.
You might use SSI if you had an existing set of documents to which
you wanted to add modification dates. You might also have a file
you want to iNClude in a number of your pages-perhaps to act as
a header or footer. You could just use the SSI iNClude command
on each of those pages, instead of copying the document into each
page manually. When available, Server-Side INCludes provide a
good way to make simple pages more interesting.
<P>
Before Server-Side INCludes were available, a CGI program was
needed in order to automatically generate the last modification
date text or to add a generic footer to all pages.
<P>
Your particular web server might have additional directives that
you can use. Check the documentation that came with it for more
information.<BR>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Tip</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
If you'd like more information about Server-Side INCludes, check out the following Web site:</BLOCKQUOTE>
<BLOCKQUOTE>
<TT>http://www.sigma.net/tdunn/</TT>
</BLOCKQUOTE>
<BLOCKQUOTE>
Tim Dunn has created a nice site that documents some of the more technical aspects of Web sites.</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Caution</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
I would be remiss if I didn't mention the down side of Server-Side INCludes. They are very processor intensive. If you don't have a high-powered computer running your web server and you expect to have a lot of traffic, you might want to limit the number
of documents that use Server-Side INCludes.</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<H2><A NAME="HTMLForms"><FONT SIZE=5 COLOR=#FF0000>
HTML Forms</FONT></A></H2>
<P>
HTML forms are designed to let a web page designer interact with
users by letting them fill out a form. The form can be composed
of elements such as input boxes, buttons, checkboxes, radio buttons,
and selection lists. All of the form elements are specified using
HTML tags surrounded by a set of <TT><FORM>..</FORM></TT>
tags. You can have more than one form per HTML document.
<P>
There are several modifiers or options used with the <TT><FORM></TT>
tag. The two most important are <TT>METHOD</TT>
and <TT>ACTION</TT>:
<UL>
<LI><B>METHOD</B>-Specifies the manner in which form information
is passed to the CGI scripts. The normal values are either <TT>GET</TT>
or <TT>POST</TT>. See "Handling
Form Information" later in this chapter.
<LI><B>ACTION</B>-Specifies the URL of the CGI script that will
be invoked when the submit button is clicked. You could also specify
an email address by using the mailto: notation. For example, sending
mail would be accomplished by <TT>ACTION="mailto:medined@planet.net"</TT>
and invoking a CGI script would be accomplished by <TT>ACTION="/cgi-bin/feedback.pl".</TT>
</UL>
<P>
Most field elements are defined using the <TT><INPUT></TT>
tag. Like the <TT><FORM></TT>
tag, <TT><INPUT></TT> has several
modifiers. The most important are:
<UL>
<LI><B>CHECKED</B>-Specifies that the checkbox or radio button
being defined is selected. This modifier should only be used when
the element type is <TT>checkbox</TT>
or <TT>radio</TT>.
<LI><B>NAME</B>-Specifies the name of a form element. Most form
elements need to have unique names. You'll see in the "Handling
Form Information" section later in this chapter that your
CGI script will use the element names to access form information.
<LI><B>MAXLENGTH</B>-Specifies the maximum number of characters
that the user can enter into a form element. If <TT>MAXLENGTH</TT>
is larger than <TT>SIZE</TT>, the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -