📄 ch8.htm
字号:
<HR>
<BLOCKQUOTE>
<B>Listing 8.5. Comments form.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><FORM ACTION="comments.cgi"
METHOD=POST><BR>
<P> In order that we may continue to provide a high quality
World Wide Web service,<BR>
please take the time to fill in this form.</P><BR>
<DL><BR>
<DT>Your surname (family name):<BR>
<DD><INPUT NAME="surname" SIZE=20 MAXLENGTH=60>
<BR>
<DT>Your first name (given name):<BR>
<DD><INPUT NAME="forename" SIZE=20 MAXLENGTH=60>
<BR>
</DL><BR>
<P>Your title:<BR>
[<INPUT TYPE=RADIO NAME="title" VALUE="Mr">
Mr.] <BR>
[<INPUT TYPE=RADIO NAME="title" VALUE="Ms">
Ms.] <BR>
[<INPUT TYPE=RADIO NAME="title" VALUE="Mrs">
Mrs.] <BR>
[<INPUT TYPE=RADIO NAME="title" VALUE="Miss">
Miss.] <BR>
[<INPUT TYPE=RADIO NAME="title" VALUE="Dr">
Dr.] <BR>
[<INPUT TYPE=RADIO NAME="title" VALUE="">
Other.] </P><BR>
<P> Please tell us what you thought of this Web site. Select
the checkboxes which you agree with: </P><BR>
<UL><BR>
<LI><INPUT TYPE=chECKBOX NAME="opinion" VALUE="understandable">
<BR>
The text was understandable.<BR>
<LI><INPUT TYPE=chECKBOX NAME="opinion" VALUE="navigable">
<BR>
I found it easy to find my way through the Web site. <BR>
<LI><INPUT TYPE=chECKBOX NAME="opinion" VALUE="stylish">
<BR>
I was impressed by the style and presentation.<BR>
</UL><BR>
<P> <INPUT TYPE="HIDDEN" NAME="pages"
VALUE="brochure"><BR>
Please add any other comments:<BR>
<TEXTAREA NAME="feedback" ROWS=5 COLS=40><BR>
I think your brochure is:<BR>
</TEXTAREA> <BR>
</P><BR>
<P> <INPUT TYPE=SUBMIT VALUE="Send comments">
<BR>
<INPUT TYPE=RESET VALUE="Clear form"> </P</FONT></TT>>
<BR>
<TT><FONT FACE="Courier"></FORM></FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
Other sample forms can be found at Web sites all over the world.
Simply use the "View Source" facility of your browser
to see the HTML tags that make up a form you find.
<H2><A NAME="HandlingFormSubmissions"><FONT SIZE=5 COLOR=#FF0000>Handling
Form Submissions</FONT></A></H2>
<P>
Handling Web form data submissions using CGI can be conveniently
divided in to two procedures. First, the CGI program must accept
the submitted form data from the Web server (the HTTP server).
The algorithm for this procedure depends on the choice of submission
method. In CGI, the submission method is given by the environment
variable <TT><FONT FACE="Courier">REQUEST_TYPE</FONT></TT> and
will be either <TT><FONT FACE="Courier">GET</FONT></TT> or <TT><FONT FACE="Courier">POST</FONT></TT>.
Second, the CGI program must decode the submitted data before
it can be used by other procedures.
<H3><A NAME="REQUEST_TYPEsGETversusPOST"><TT><FONT SIZE=4 FACE="Courier">REQUEST_TYPE</FONT></TT><FONT SIZE=4>s
</FONT><TT><FONT SIZE=4 FACE="Courier">GET</FONT></TT><FONT SIZE=4>
versus </FONT><TT><FONT SIZE=4 FACE="Courier">POST</FONT></TT></A>
</H3>
<P>
The names of the two <TT><FONT FACE="Courier">REQUEST_TYPE</FONT></TT>s
<TT><FONT FACE="Courier">GET</FONT></TT> and <TT><FONT FACE="Courier">POST</FONT></TT>
correspond to the HTTP mechanism used in each case. They have
different characteristics and so need different treatment.
<H4>Differences between <TT><FONT FACE="Courier">GET</FONT></TT>
and <TT><FONT FACE="Courier">POST</FONT></TT></H4>
<P>
A <TT><FONT FACE="Courier">GET</FONT></TT> form submission is
performed by fetching a URL made up of
<UL>
<LI><FONT COLOR=#000000>The location of the CGI form handler program</FONT>
<LI><FONT COLOR=#000000>Followed by a question mark (</FONT><TT><FONT FACE="Courier">"?"</FONT></TT>)
<LI><FONT COLOR=#000000>Followed by the form data encoded as described
in the section "Decoding </FONT><TT><FONT FACE="Courier">+</FONT></TT>
and <TT><FONT FACE="Courier">%<I>hh</I></FONT></TT> (URL-Encoding)"
</UL>
<P>
For example, the default values in the form in Listing 8.4 might
be submitted as a <TT><FONT FACE="Courier">GET </FONT></TT>of
the<TT><FONT FACE="Courier"> </FONT></TT>URL
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><A HREF="http://www.site.com/cgi-bin/select.cgi?package=text&coords=19,11">http://www.site.com/cgi-bin/select.cgi?package=text&coords=19,11</A></FONT></TT>
</BLOCKQUOTE>
<P>
Because the form data is included as part of the URL in a "<TT><FONT FACE="Courier">GET</FONT></TT>"
request, the reply to the form submission is likely to be remembered
by caching browsers and caching HTTP proxy servers. For this reason,
it is advisable to use <TT><FONT FACE="Courier">GET</FONT></TT>
as the <TT><FONT FACE="Courier">METHOD</FONT></TT> attribute of
forms that always yield the same result for the same input and
that have no side effects, such as index search forms.
<P>
A <TT><FONT FACE="Courier">POST</FONT></TT> form submission is
performed by sending the form data encoded as described in the
section "Decoding <TT><FONT FACE="Courier">+</FONT></TT>
and <TT><FONT FACE="Courier">%<I>hh</I></FONT></TT> (URL-Encoding)"
as a document with the HTTP header
<BLOCKQUOTE>
<TT><FONT FACE="Courier">Content-type: application/x-www-form-urlencoded</FONT></TT>
</BLOCKQUOTE>
<P>
to the server giving the location of the CGI form handler program
as the URL.
<P>
For example, a submission from the form in Listing 8.1 might reach
the Web server as this HTTP transaction:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">POST http://www.site.com HTTP/1.0<BR>
User-agent: Pip/2.53<BR>
Content-type: application/x-www-form-urlencoded <BR>
Content-length: 50<BR>
<BR>
fullname=Charles+Dickens&email=dickens%40literary.org.uk</FONT></TT>
</BLOCKQUOTE>
<P>
Because this operation is easily distinguished from an ordinary
URL fetch and the form data is in an attached document rather
than part of the URL, this sort of request is rarely remembered
by caching proxy servers. <TT><FONT FACE="Courier">POST</FONT></TT>
should be used as the <TT><FONT FACE="Courier">METHOD</FONT></TT>
attribute of forms that are intended to have a useful side effect
every time they are submitted, or that can produce different results
for the same input.
<H4>Accepting <TT><FONT FACE="Courier">GET</FONT></TT> Type Requests
</H4>
<P>
From within a CGI program, form data submitted with the <TT><FONT FACE="Courier">FORM</FONT></TT>
<TT><FONT FACE="Courier">METHOD</FONT></TT> attribute <TT><FONT FACE="Courier">GET</FONT></TT>
is encoded in the environment variable <TT><FONT FACE="Courier">QUERY_STRING</FONT></TT>.
From a C program, this could be referenced by using a call to
the <TT><FONT FACE="Courier">getenv</FONT></TT> library routine,
as shown in Listing 8.6.
<HR>
<BLOCKQUOTE>
<B>Listing 8.6. CGI environment variables in C.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">encodedQuery=getenv("QUERY_STRING");</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
In a Perl script, it is available in the <TT><FONT FACE="Courier">%ENV</FONT></TT>
associative array:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">$encodedQuery=$ENV{"QUERY_STRING"};</FONT></TT>
</BLOCKQUOTE>
<P>
Because the form data is submitted in an environment variable
by way of a URL, <TT><FONT FACE="Courier">GET</FONT></TT> form
submissions are subject to smaller system defined size limits.
The <TT><FONT FACE="Courier">GET</FONT></TT> method should not
be used for forms where the expected data will be very large.
<H4>Accepting <TT><FONT FACE="Courier">POST</FONT></TT> Type Requests
</H4>
<P>
From the point of view of a CGI program, form data submitted with
the <TT><FONT FACE="Courier">POST</FONT></TT> method is an encoded
stream of characters in the standard input of the program. If
a Content-length: header was supplied by the form browser, the
number of characters that need to be read from the standard input
is available to the CGI program in the <TT><FONT FACE="Courier">CONTENT_LENGTH</FONT></TT>
environment variable. No more than <TT><FONT FACE="Courier">CONTENT_LENGTH</FONT></TT>
characters should be read in. In the event that there is no <TT><FONT FACE="Courier">CONTENT_LENGTH</FONT></TT>
environment variable, the CGI program should read characters from
standard input until an "end-of-file" or other exception
occurs. In a Perl script, <TT><FONT FACE="Courier">POST</FONT></TT>ed
form data would be retrieved with a procedure similar to Listing
8.7.
<HR>
<BLOCKQUOTE>
<B>Listing 8.7. Accepting <FONT SIZE=2 FACE="MCPdigital-B">POST</FONT>
type requests in Perl.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">$encodedQuery=""; #
The encoded form data will be appended to this string<BR>
$charsRemaining=65536; # This CGI program
will truncate the encoded form data after 64Kbytes<BR>
#
unless the Content-length: is specified.<BR>
$charsRemaining=$ENV{"CONTENT_LENGTH} if $ENV{"CONTENT_LENGTH"};
<BR>
while ($charsRemaining--) {<BR>
$encodedQuery.=getc;<BR>
}</FONT></TT>
</BLOCKQUOTE>
<HR>
<H4>Accepting <TT><FONT FACE="Courier">ISINDEX</FONT></TT> Requests
</H4>
<P>
Although not strictly forms, queries from <TT><FONT FACE="Courier"><ISINDEX></FONT></TT>
Web pages behave very similarly to <TT><FONT FACE="Courier">FORM
METHOD=GET</FONT></TT> submissions. The only difference is that
the "Separating data from different form components"
step of the form data decoding procedure is not required.
<H3><A NAME="FormDataDecoding">Form Data Decoding</A></H3>
<P>
Form submissions, whether they are sent using <TT><FONT FACE="Courier">GET</FONT></TT>
or <TT><FONT FACE="Courier">POST</FONT></TT>, have the same encoding
and can be unpacked using the same procedures.
<H4>Separating Data from Different Form Components</H4>
<P>
Except in the case of <TT><FONT FACE="Courier"><ISINDEX></FONT></TT>
queries, the encoded submission is made up of pairs of input component
<TT><FONT FACE="Courier">NAME</FONT></TT> with input component
contents joined by an equal sign (<TT><FONT FACE="Courier">=</FONT></TT>)
and delimited by ampersands (<TT><FONT FACE="Courier">&</FONT></TT>).
For example, the default contents of the form in Listing 8.8 would
be encoded as shown.
<BLOCKQUOTE>
<TT><FONT FACE="Courier">comments=None&choice=Good&where=HomePage</FONT></TT>
</BLOCKQUOTE>
<HR>
<BLOCKQUOTE>
<B>Listing 8.8. Separating data from different form components
in Perl.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><FORM ACTION="test.cgi"
METHOD=POST><BR>
<P>Comments: <TEXTAREA NAME="comments" ROWS=3
COLS=40>None</TEXTAREA></P><BR>
<SELECT NAME="choice" SIZE=4><BR>
<OPTION>Excellent<BR>
<OPTION SELECTED>Good<BR>
<OPTION>Average<BR>
<OPTION>Poor<BR>
</SELECT><BR>
<INPUT TYPE=HIDDEN NAME="where" VALUE="HomePage">
<BR>
</FORM></FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
The Perl procedure in Listing 8.9 will fill an associative array
with the contents of each form component listed by the form component
<TT><FONT FACE="Courier">NAME</FONT></TT> attribute.
<HR>
<BLOCKQUOTE>
<B>Listing 8.9. Filling a Perl associative array with form data.
<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">foreach (split("&", $encodedQuery))
{<BR>
($name,$contents) = split("=");
<BR>
$encodedForm{$name}=$contents;<BR>
}</FONT></TT>
</BLOCKQUOTE>
<HR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -