⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch20.htm

📁 prrl 5 programs codes in the book
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<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&nbsp;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>&lt;B&gt;</TT> means

that the following text should be displayed in <B>bold</B>. To

stop the bold text, use the <TT>&lt;/B&gt;</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 &quot;correct&quot;. The <TT>&lt;HEAD&gt;..&lt;/HEAD&gt;</TT>

set of tags surround the header information for each document.

Inside the header, you can specify a document title with the <TT>&lt;TITLE&gt;..&lt;/TITLE&gt;</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>&lt;TITLE&gt;</TT> is the same as <TT>&lt;title&gt;</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>&lt;BODY&gt;..&lt;/BODY&gt;</TT>

tags. Inside the document's body, you specify text headings by

using a set of <TT>&lt;H1&gt;..&lt;/H1&gt;</TT>

tags. Changing the number after the H changes the heading level.

For example, <TT>&lt;H1&gt;</TT> is

the first level. <TT>&lt;H2&gt;</TT>

is the second level, and so on.

<P>

You can use the <TT>&lt;P&gt;</TT>

tag to indicate paragraph endings or use the <TT>&lt;BR&gt;</TT>

to indicate a line break. The <TT>&lt;B&gt;..&lt;/B&gt;</TT>

and <TT>&lt;I&gt;..&lt;/I&gt;</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>&lt;HTML&gt;..&lt;/HTML&gt;</TT>

tags. For example:

<BLOCKQUOTE>

<PRE>

&lt;HTML&gt;

&lt;HEAD&gt;&lt;TITLE&gt;This is the Title&lt;/TITLE&gt;&lt;/HEAD&gt;

&lt;BODY&gt;

&lt;H1&gt;This is a level one header&lt;/H1&gt;

This is the first paragraph.

&lt;P&gt;This is the second paragraph and it has &lt;I&gt;italic&lt;/I&gt; text.

&lt;H2&gt;This is a level two header&lt;/H2&gt;

This is the third paragraph and it has &lt;B&gt;bold&lt;/B&gt; text.

&lt;/BODY&gt;

&lt;/HTML&gt;

</PRE>

</BLOCKQUOTE>

<P>

Most of the time, you will be inserting or modifying text inside

the <TT>&lt;BODY&gt;..&lt;/BODY&gt;</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 &quot;HTTP Headers&quot; in <A HREF="ch19.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch19.htm" >Chapter 19</A>, &quot;What Is CGI?,&quot; 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&nbsp;&nbsp;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>&lt;!--#config timefmt=&quot;%c&quot;--&gt;</TT>

</TD><TD WIDTH=307>Changes the format used to display dates.</TD>

</TR>

<TR><TD WIDTH=283><TT>&lt;!--#config sizefmt=&quot;%d bytes&quot;--&gt;</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>&lt;!--#config errmsg=&quot;##ERROR!##&quot;--&gt;</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>&lt;!--#echo var=?--&gt;</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>&lt;!--#echo var=&quot;DOCUMENT_NAME&quot;--&gt;</TT>

</TD><TD WIDTH=307>Displays the full path and filename of the current document.

</TD></TR>

<TR><TD WIDTH=283><TT>&lt;!--#echo var=&quot;DOCUMENT_URI&quot;--&gt;</TT>

</TD><TD WIDTH=307>Displays the virtual path and filename of the current document.

</TD></TR>

<TR><TD WIDTH=283><TT>&lt;!--#echo var=&quot;LAST_MODIFIED&quot;--&gt;</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>&lt;!--#echo var=&quot;DATE_LOCAL&quot;--&gt;</TT>

</TD><TD WIDTH=307>Displays the date and time using the local time zone.

</TD></TR>

<TR><TD WIDTH=283><TT>&lt;!--#echo var=&quot;DATE_GMT&quot;--&gt;</TT>

</TD><TD WIDTH=307>Displays the date and time using GMT.</TD>

</TR>

<TR><TD WIDTH=283><TT>&lt;!--#exec cgi=&quot;/cgi-bin/ssi.exe&quot;--&gt;</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>&lt;!--#flastmod virtual=&quot;/docs/demo/ssi.txt&quot;--&gt;</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>&lt;!--#flastmod file=&quot;ssi.txt&quot;--&gt;</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>&lt;!--#fsize virtual=&quot;/docs/demo/ssi.txt&quot;--&gt;</TT>

</TD><TD WIDTH=307>Displays the size of the specified file given a virtual path.

</TD></TR>

<TR><TD WIDTH=283><TT>&lt;!--#fsize file=&quot;ssi.txt&quot;--&gt;</TT>

</TD><TD WIDTH=307>Displays the size of the specified file given a relative path.

</TD></TR>

<TR><TD WIDTH=283><TT>&lt;!--#iNClude virtual=&quot;/docs/demo/ssi.txt&quot;--&gt;</TT>

</TD><TD WIDTH=307>Displays a file given a virtual path.</TD>

</TR>

<TR><TD WIDTH=283><TT>&lt;!--#iNClude file=&quot;ssi.txt&quot;--&gt;</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>&lt;FORM&gt;..&lt;/FORM&gt;</TT>

tags. You can have more than one form per HTML document.

<P>

There are several modifiers or options used with the <TT>&lt;FORM&gt;</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 &quot;Handling

Form Information&quot; 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=&quot;mailto:medined@planet.net&quot;</TT>

and invoking a CGI script would be accomplished by <TT>ACTION=&quot;/cgi-bin/feedback.pl&quot;.</TT>

</UL>

<P>

Most field elements are defined using the <TT>&lt;INPUT&gt;</TT>

tag. Like the <TT>&lt;FORM&gt;</TT>

tag, <TT>&lt;INPUT&gt;</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 &quot;Handling

Form Information&quot; 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 + -