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

📄 ch20.htm

📁 this is a book on pearl , simple example with explanation is given here. it could be beneficial for
💻 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 informationfrom HTML forms. This chapter gives you an extremely brief overviewof HTML and Forms. Next you see how the form information is sentto CGI programs. After being introduced to form processing, aGuest 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 usedby web programmers to describe the contents of a web page. Itis not a programming language. You simply use HTML to indicatewhat a certain chunk of text is-such as a paragraph, a headingor specially formatted text. All HTML directives are specifiedusing matched sets of angle brackets and are usually called <I>tags</I>.For example <TT>&lt;B&gt;</TT> meansthat the following text should be displayed in <B>bold</B>. Tostop the bold text, use the <TT>&lt;/B&gt;</TT>directive. Most HTML directives come in pairs and surround theaffected text.<P>HTML documents need to have certain tags in order for them tobe 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 byusing 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> isthe 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 surroundedby 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 insidethe <TT>&lt;BODY&gt;..&lt;/BODY&gt;</TT>tags.<P>That's enough about generic HTML. The next section discusses Server-SideINCludes. Today, Server-Side INCludes are replacing some basicCGI 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 serversis that of Server-Side INCludes or SSI. SSI is a set of fuNCtionsbuilt into web servers that give HTML developers the ability toinsert data into HTML documents using special directives. Thismeans that you can have dynamic documents without needing to createfull CGI programs.<P>The inserted information can take the form of a local file ora file refereNCed by a URL. You can also iNClude information froma limited set of variables-similar to environmental variables.Finally, you can execute programs that can insert text into thedocument.<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" >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 theserver to know that it needs to look for Server-Side directives.The file extension is dependent on server configuration, but shtmlis 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 serversthat do not support them.<P>Table 20.1 shows a partial list of SSI directives supported bythe <TT>webSite</TT> server from O'Reilly.Not all Web servers will support all of the directives in thetable. You need to check the documentation of your web serverto 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 whichyou wanted to add modification dates. You might also have a fileyou want to iNClude in a number of your pages-perhaps to act asa header or footer. You could just use the SSI iNClude commandon each of those pages, instead of copying the document into eachpage manually. When available, Server-Side INCludes provide agood way to make simple pages more interesting.<P>Before Server-Side INCludes were available, a CGI program wasneeded in order to automatically generate the last modificationdate text or to add a generic footer to all pages.<P>Your particular web server might have additional directives thatyou can use. Check the documentation that came with it for moreinformation.<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 withusers by letting them fill out a form. The form can be composedof elements such as input boxes, buttons, checkboxes, radio buttons,and selection lists. All of the form elements are specified usingHTML 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 informationis passed to the CGI scripts. The normal values are either <TT>GET</TT>or <TT>POST</TT>. See &quot;HandlingForm Information&quot; later in this chapter.<LI><B>ACTION</B>-Specifies the URL of the CGI script that willbe invoked when the submit button is clicked. You could also specifyan email address by using the mailto: notation. For example, sendingmail 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 severalmodifiers. The most important are:<UL><LI><B>CHECKED</B>-Specifies that the checkbox or radio buttonbeing defined is selected. This modifier should only be used whenthe element type is <TT>checkbox</TT>or <TT>radio</TT>.<LI><B>NAME</B>-Specifies the name of a form element. Most formelements need to have unique names. You'll see in the &quot;HandlingForm Information&quot; section later in this chapter that yourCGI script will use the element names to access form information.<LI><B>MAXLENGTH</B>-Specifies the maximum number of charactersthat the user can enter into a form element. If <TT>MAXLENGTH</TT>is larger than <TT>SIZE</TT>, theuser will be able to scroll to access text that is not visible.<LI><B>TYPE</B>-Specifies the type of input field. The most importantfield types are <TT>checkbox</TT>,<TT>hidden</TT>, <TT>password</TT>,<TT>radio</TT>, <TT>reset</TT>,<TT>submit</TT>, and <TT>text</TT>.<LI><B>SIZE</B>-Specifies the size of an input field.<LI><B>VALUE</B>-Specifies the default value for a field. The<TT>VALUE</TT> modifier is requiredfor radio buttons.</UL><P>Let's look at how to specify a plain text field:<BLOCKQUOTE><PRE>&lt;INPUT TYPE=text NAME=lastName VALUE=WasWaldo SIZE=25 MAXLENGTH=50&gt;</PRE>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -