📄 ch04.htm
字号:
of information is the MIME type. It is very important that you specify the correct
MIME type for your HTML data. The first line of your output should always be <TT>Content-Type:
text/html;</TT>. This line tells the browser what kind of data to expect. Fortunately,
by using <TT>CGI::Form</TT>, you need not worry about this detail because it is the
default type. Simply calling the method <TT>header</TT> returns the correct MIME
type for you.</P>
<P>In addition to the document header, you will also need to correctly specify information
about the types of any forms that are included in the document. This section will
describe the two different types of forms as well.</P>
<PRE><FONT COLOR="#0066FF">print $q->header();
</FONT></PRE>
<P><B><TT>The HTML Header</TT></B> The next step is to construct the HTML header.
The HTML header usually consists of just a title but may also include things that
go into the <TT><BODY></TT> tag, such as background and foreground colors.
Using the <TT>start_html</TT> method in <TT>CGI::Form</TT> provides a nice way to
incorporate all of your HTML header information. In this example, I am starting an
HTML document with a nice lavender background color (specified as a hexadecimal RGB
triplet).</P>
<PRE><FONT COLOR="#0066FF">print $q->start_html(-title=>"Customer Survey Page",-author=>'bdeng@adobe.com',
-BGCOLOR=> "DDDDFF");
</FONT></PRE>
<P>Not included in this function, however, is the new HTML 3.2<TT> <!DOCTYPE></TT>
directive. This directive is what all HTML 3.2 conforming documents should begin
with. This directive is actually part of SGML, but it is used to identify the HTML
document as such. More information about HTML and SGML can be found at <TT>http://www.w3.org/pub/WWW/MarkUp</TT>.
Hopefully, the <TT>start_html</TT> method will incorporate this standard at some
point. <B><TT>Types of Forms</TT></B> Two different MIME types are currently defined
for HTML forms. The first is <TT>application/ x-www-form-urlencoded.</TT> This is
the original type defined for simple forms. The second is <TT>multipart/form-data</TT>.
This type was introduced by Netscape 2.0 for the transfer of large chunks of data.
It is important that you choose your form type appropriately. Generally speaking,
if you need to support many different types of browsers, some of which may not handle
<TT>multipart/ form-data</TT>, choose the original MIME type. However, if you want
to use such fields as the file upload field, you must choose <TT>multipart/form-data</TT>.
Eventually, all browsers should support <TT>multipart/form-data</TT>, at which point,
it would be the obvious choice.</P>
<P>There are two methods within <TT>CGI::Form</TT> for starting your form--<TT>startform()</TT>
and <TT>start_multipart_form()</TT>. You should use whichever one corresponds to
the type of form you are creating. These methods take the optional parameters <TT>$method</TT>,
<TT>$request</TT>, and <TT>$encoding</TT>. You shouldn't need to worry about the
<TT>$encoding</TT> parameter because the method you choose will specify the appropriate
encoding mechanism for your form. <TT>$method</TT> defaults to <TT>POST</TT>, and
<TT>$action</TT> defaults to the current CGI script, which is generally what you
want.
<H4 ALIGN="CENTER"><A NAME="Heading20"></A><FONT COLOR="#000077">Constructing Elements</FONT></H4>
<P>You have seen a few examples of how to define fields using <TT>CGI::Form</TT>.
Now let's look at an example of a complete form. What you'll do is create a general
questionnaire form for a restaurant. You'll ask the user these things: What did you
order? (text field)</P>
<P>How was the service? (radio group)</P>
<P>How did the food taste? (radio group)</P>
<P>What is your age group? (popup menu)</P>
<PRE><FONT COLOR="#0066FF">
Would you come back again? (checkbox)
</FONT></PRE>
<P>The Perl code in Listing 4.1 generates these fields using a <TT>multipart/form-data</TT>
type form.
<H3 ALIGN="CENTER"><A NAME="Heading21"></A><FONT COLOR="#000077">Listing 4.1. Perl
code for customer questionnaire.</FONT></H3>
<PRE><FONT COLOR="#0066FF">
use CGI::Form;
$q = new CGI::Form;
print "<H1>Customer Survey</H1>\n";
print $q->start_multipart_form();
print "What did you order? ";
print $q->textfield( -name=>'order', -value=>"", -maxlength=>30, -size=>30 );
print "<BR>"; # Line break
print "How was the service? (5-good,1-bad) ";
print $q->radio_group( -name=>'serviceRating', -values=>[`1','2','3','4','5'],
-default=>'3' );
print "<BR>";
print "How did the food taste? (5-good,1-bad) ";
print $q->radio_group( -name=>'tasteRating', -values=>[`1','2','3','4','5'],
-default=>'3' );
print "<BR>";
print "What age group are you in? ";
@ages=( `child', `teen', `20s', `30s', `40s', `50s', `senior' );
print $q->popup_menu( -name=>'ageGroup', -values=>\@ages );
print "<BR>";
print $q->checkbox( -name=>'comeAgain', -checked=>'checked', -value=>'yes',
-label=>"I would come back again " );
print "<BR><BR>";
print $q->submit( -name=>'Action', -value=>'Submit' );
print $q->reset();
print $q->endform();
</FONT></PRE>
<P>This form appears in the browser, as shown in Figure 4.1.</P>
<P>You can include more than one form on a single HTML page. Each form can be associated
with separate actions or the same action. <BR>
<BR>
<B><TT>Figure 4.1.</TT></B><TT> </TT>The questionnaire as it appears in the browser.
<H4 ALIGN="CENTER"><A NAME="Heading22"></A><FONT COLOR="#000077">Included Files</FONT></H4>
<P>One feature that is provided by some Web servers is server-side includes. You
can configure your Web server in such a way that files of certain extensions (let's
use the common one, <TT>.shtml</TT>) are parsed by the server before they are sent
down to the client. There are two directives that tell the server to perform a specific
operation with the file.</P>
<PRE><FONT COLOR="#0066FF"><!..#echo....>
</FONT></PRE>
<P>and</P>
<PRE><FONT COLOR="#0066FF"><!..#include...>
</FONT></PRE>
<P>The server-side include feature is used to dynamically include nested HTML files.
This feature enables some limited dynamically created content. The <TT>include</TT>
directive is used to include other HTML files, much like a C program including header
files. The <TT>echo</TT> directive allows you to dynamically display certain variables.
The variables that can be displayed using the <TT>echo</TT> directive are as follows:</P>
<PRE><FONT COLOR="#0066FF">DATE_GMT
DATE_LOCAL
DOCUMENT_NAME
DOCUMENT_URI
LAST_MODIFIED
QUERY_STRING_UNESCAPED
</FONT></PRE>
<P>For more information on SSIs, refer to HTML & CGI Unleashed (Sams, 1995).</P>
<P>As you saw previously, all of the HTML that you generate from your Perl script
is dynamic. It would be very easy to incorporate this kind of information using simple
print statements. For example, to print the local date, instead of using an <TT>echo</TT>
directive you might write the following:</P>
<PRE><FONT COLOR="#0066FF">print "The current local time is: " . localtime() . "\n";
</FONT></PRE>
<P>Or, to print Greenwich Mean Time, you could write:</P>
<PRE><FONT COLOR="#0066FF">print "The current time in Greenwich is: " . gmtime() . "\n";
</FONT></PRE>
<P>The <TT>include</TT> directive of server-side includes can be easily implemented
in Perl code, as in List-ing 4.2.
<H3 ALIGN="CENTER"><A NAME="Heading23"></A><FONT COLOR="#000077">Listing 4.2. Perl
subroutine for including another HTML file.</FONT></H3>
<PRE><FONT COLOR="#0066FF">
sub includeHTMLFile {
local($theHTMLFile)=@_;
local($thePathToHTMLFile)="$ENV{`DOCUMENT_ROOT'}/$theHTMLFile";
open(IN,"< $thePathToHTMLFile") ||
die "Cannot open file: $thePathToHTMLFile for read!\n";
print(<IN>);
close(IN);
}
</FONT></PRE>
<P>In this example, the CGI environment variable <TT>DOCUMENT_ROOT</TT> determines
the location of the included file. This is one of several well-defined environment
variables for use in CGI programming.</P>
<P>You can extend the function in Listing 4.2 to process <TT>.shtml</TT> files and
perform the proper action on the directives. Listing 4.3 shows a CGI program that
uses the <TT>PATH_INFO</TT> environment variable to specify the <TT>.shtml</TT> file
to parse. This CGI program can be specified as <TT>http://foo.bar.com/cgi-bin/parsessi.pl/included.shtml</TT>.
<TT>PATH_INFO</TT> will contain <TT>/included.shtml</TT>, and that document will
be returned with the proper SSI parsing having taken place.
<H3 ALIGN="CENTER"><A NAME="Heading24"></A><FONT COLOR="#000077">Listing 4.3. parsessi.pl
CGI program for including and parsing a .shtml file.</FONT></H3>
<PRE><FONT COLOR="#0066FF">
use CGI::Form;
$q = new CGI::Form;
print $q->header();
print $q->start_html(-title=>"Include Example",-author=>'bdeng@adobe.com',
-BGCOLOR=>"DDDDFF");
print "<H1>Include Example</H1>\n";
if ($ENV{`PATH_INFO'} ne "") {
&includeHTMLFile($ENV{`PATH_INFO'});
} else {
print "<P>No file was specified to parse";
}
print $q->end_html();
sub includeHTMLFile {
local($theHTMLFile)=@_;
local($thePathToHTMLFile)="$ENV{`DOCUMENT_ROOT'}${theHTMLFile}";
open(IN,"< $thePathToHTMLFile") ||
die "Cannot open file: $thePathToHTMLFile for read!\n";
local(@lines)=<IN>;
close(IN);
foreach(@lines) {
if (/\<!\.\.\#include(.*)\.\.\>/) {
@parms=split(/ /,$1);
foreach(@parms) {
($key,$val)=split(/=/,$_);
$keys{$key}=$val;
}
&includeHTMLFile($keys{`file'});
} elsif (/\<\!\.\.\#echo(.*)\.\.\>/) {
@parms=split(/ /,$1);
foreach(@parms) {
($key,$val)=split(/=/,$_);
$keys{$key}=$val;
}
print $ENV{$keys{`var'}};
} else {
print;
}
}
}
</FONT></PRE>
<P>Another method of incorporating raw HTML within your Perl script is to use what's
known as here documents. Here documents are generally used to enclose large pieces
of raw text and are constructed using <TT>ENDOFTEXT</TT>, where <TT>ENDOFTEXT</TT>
is a label that you can arbitrarily choose. The rest of the Perl script will not
be interpreted, but rather treated as plain text until the string <TT>ENDOFTEXT</TT>
is found.
<DL>
<DT></DT>
</DL>
<H3 ALIGN="CENTER">
<HR WIDTH="83%">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -