📄 ch04.htm
字号:
<BR>
<FONT COLOR="#000077">NOTE:</FONT></H3>
<BLOCKQUOTE>
<P><TT>ENDOFTEXT</TT> must appear at the beginning of the line. All references to
it after the first column are ignored.<BR>
<HR>
</BLOCKQUOTE>
<P>An example of using a here document for embedding HTML text follows:</P>
<PRE><FONT COLOR="#0066FF">print <<ENDOFMYHTML;
<H3>Important Information</H3>
<P>This information is <B>very</B> important! Everything up until the
<BLINK>ENDOFTEXT</BLINK> line will appear in this page.
<HR>
ENDOFTEXT
</FONT></PRE>
<H4 ALIGN="CENTER"><A NAME="Heading26"></A><FONT COLOR="#000077">Closing Up</FONT></H4>
<P>To end your HTML document, you should call the <TT>end_html()</TT> method, which
inserts the <TT></BODY></TT> and <TT></HTML></TT> tags for you.</P>
<PRE><FONT COLOR="#0066FF">print $q->end_html();
</FONT></PRE>
<H3 ALIGN="CENTER"><A NAME="Heading27"></A><FONT COLOR="#000077">Heady StuffGenerating
More Advanced (Possibly Dynamic) HTML</FONT></H3>
<P>The concepts you have seen so far are fairly straightforward. Sometimes, you might
want to do more than just display a few fields and process the Submit button. The
next step is to take the initial building blocks and produce more complicated forms
using other HTML tags. This section discusses some more advanced HTML generation
using the Perl5 libraries.
<H4 ALIGN="CENTER"><A NAME="Heading28"></A><FONT COLOR="#000077">Other Elements Within
Elements</FONT></H4>
<P>You also might want to incorporate form fields within other types of tags. This
is easily done by combining the HTML for your special tags together with the output
from these methods to produce formatted forms. This section discusses these approaches
to work around some limitations. <B><TT>Using Tables for Better Field Layout</TT></B>
One major drawback in HTML is the lack of ability to align fields. Suppose you wish
to lay out your fields along a grid. If you desire this kind of effect, the trick
is to use tables, which are available in HTML 3.2. Tables are defined using the <TT><TABLE></TT>
and <TT></TABLE></TT> tags. Rows within a table are defined using the <TT><TR></TT>
and <TT></TR></TT> tags. Columns within the row are defined using the <TT><TD></TT>
and <TT></TD></TT> tags. Header columns within the row are defined using the
<TT><TH></TT> and <TT></TH></TT> tags. Let's take the restaurant questionnaire
form and make it look a bit nicer using a table. Although the <TT><TABLE></TT>
tag provides a <TT>BORDER</TT> option, you probably won't want to use borders around
your form fields. Borders are more suitable for displaying a table of data. The text
prompts should go in the first column, and the others should go in the second column.
You no longer need the line breaks because these are inferred by the table row definitions.
The Perl code will now look something like Listing 4.4.
<H3 ALIGN="CENTER"><A NAME="Heading29"></A><FONT COLOR="#000077">Listing 4.4. Perl
code that emits a form formatted using the <TABLE> object.</FONT></H3>
<PRE><FONT COLOR="#0066FF">use CGI::Form;
$q = new CGI::Form;
print "<H1>Customer Survey Using Tables</H1>\n";
print $q->start_multipart_form();
print "<TABLE>\n<TR>\n<TD>\n";
print "What did you order? ";
print "</TD>\n<TD>\n";
print $q->textfield( -name=>'order', -value=>"", -maxlength=>30, -size=>30 );
print "</TD>\n</TR>\n<TR>\n<TD>\n";
print "How was the service? (5-good,1-bad) ";
print "</TD>\n<TD>\n";
print $q->radio_group( -name=>'serviceRating', -values=>[`1','2','3','4','5'],
-default=>'3' );
print "</TD>\n</TR>\n<TR>\n<TD>\n";
print "How did the food taste? (5-good,1-bad) ";
print "</TD>\n<TD>\n";
print $q->radio_group( -name=>'tasteRating', -values=>[`1','2','3','4','5'],
-default=>'3' );
print "</TD>\n</TR>\n<TR>\n<TD>\n";
print "What age group are you in? ";
@ages=( `child', `teen', `20s', `30s', `40s', `50s', `senior' );
print "</TD>\n<TD>\n";
print $q->popup_menu( -name=>'ageGroup', -values=>\@ages );
print "</TD>\n</TR>\n<TR>\n<TD>\n";
print $q->checkbox( -name=>'comeAgain', -checked=>'checked', -value=>'yes',
-label=>"I would come back again " );
print "</TD>\n</TR>\n<TR>\n<TD>\n";
print $q->submit( -name=>'Action', -value=>'Submit' );
print $q->reset();
print "</TABLE>";
print $q->endform();
</FONT></PRE>
<P>The form generated by this script will appear in the browser as shown in Figure
4.2. <BR>
<BR>
<B><TT>Figure 4.2.</TT></B><TT> </TT>The questionnaire, using a table, as it appears
in the browser. <BR>
<BR>
<B><TT>Using Netscape Frames</TT></B> You can also use Netscape frames to display
multiple pages within a single browser window. Frames are defined using the <TT><FRAME></TT>
tag, where each frame is associated with a single URL. You can have multiple CGI
scripts running to produce a single Web page where each script runs within its own
frame. An example HTML document that incorporates two frames running each of the
above CGI programs follows:</P>
<PRE><FONT COLOR="#0066FF"><HTML>
<FRAMESET ROWS=50%,50%>
<FRAME SRC="http://vaders/cgi-bin/wpp/testform.cgi">
<FRAME SRC="http://vaders/cgi-bin/wpp/tablform.cgi">
</FRAMESET>
</HTML>
</FONT></PRE>
<DL>
<DT><FONT COLOR="#0066FF"></FONT></DT>
</DL>
<H3 ALIGN="CENTER">
<HR WIDTH="83%">
<FONT COLOR="#0066FF"><BR>
</FONT><FONT COLOR="#000077">NOTE:</FONT></H3>
<BLOCKQUOTE>
<P>The <TT><FRAME></TT> tag is not an HTML 2.0 or 3.2 tag. It is a Netscape
extension that is also supported in Microsoft's Internet Explorer 3.0. If you are
concerned about compatibility with other browsers, you should avoid using frames
or provide a non-frame alternative.<BR>
<HR>
</BLOCKQUOTE>
<P>In this example, the page displays two horizontal frames of equal size. In the
top frame, it will execute <TT>testform.pl</TT>, and in the bottom frame, it will
execute <TT>tablform.pl</TT>. The frames are divided evenly across the page. The
resulting page of this example is shown in Figure 4.3. <BR>
<BR>
<B><TT>Figure 4.3.</TT></B><TT> </TT>Both forms displayed in separate Netscape frames.
<H4 ALIGN="CENTER"><A NAME="Heading31"></A><FONT COLOR="#000077">Other Objects: Applets,
Images, Animation, and the <EMBED> Tag</FONT></H4>
<P>Suppose you want to embed other types of objects within your Web page. You can
do this in HTML using the <TT><EMBED></TT> tag. Many different companies have
extended the definition of this tag to suit their object's needs. This section briefly
describes what kinds of embeddable objects are out there and how you might go about
incorporating them into your page, using available Perl library code.</P>
<P>Some types of embeddable objects are provided as Netscape plug-ins through the
<TT><EMBED></TT> tag. For example, you can embed an Adobe Acrobat PDF file
into your Web page if you have the PDF plug-in. You can also embed AVI files using
the sample AVI plug-in provided from Netscape. The next example shows how to do both.</P>
<PRE><FONT COLOR="#0066FF"><embed src=movies/sample.avi width=300 height=100>
<br>
<embed src=pdfs/sample.pdf width=300 height=300>
</FONT></PRE>
<P>The Adobe Acrobat Netscape plug-in is available at <TT>http://www.adobe.com</TT>.
The Netscape plug-in development kit can be found at <TT>http://home.netscape.com</TT>.
There are plenty of other Netscape plug-ins available. To see a complete list, visit
the Netscape software page at <TT>http://home.netscape.com/comprod/upgrades/index.html</TT>.
Additionally, you can embed audio and various other types of multimedia objects with
the <TT><EMBED></TT> tag.</P>
<P>Java has become a huge buzzword in the industry of late. One of the things Java
provides is the capability to download an executable applet from a Web server to
a client browser and run that applet under a secure environment. Applets can be thought
of as small application components that are useful for a specific task. These applets
can provide a higher level of interaction that allows for some interesting Web page
content. Some interesting Java applets can be found at <TT>http://www.gamelan.com</TT>.
Java applets are included within an HTML document using the <TT><APPLET></TT>
tag, as follows:</P>
<PRE><FONT COLOR="#0066FF"><APPLET CODE=JavaApplet.class WIDTH=200 HEIGHT=200>
</APPLET>
</FONT></PRE>
<P>In this example, the Java applet is compiled into a file called <TT>JavaApplet.class</TT>,
and when the browser loads the page containing this tag, <TT>JavaApplet.class</TT>
will be downloaded and executed within the context of the browser.
<H4 ALIGN="CENTER"><A NAME="Heading32"></A><FONT COLOR="#000077">What Forms Cannot
Provide</FONT></H4>
<P>You've just seen how forms can become a very valuable addition to your Web site.
I have described the HTML form as a potential cross-platform, simple user interface.
There are still some features that are missing that prevent it from replacing other
traditional user interfaces. HTTP requests and responses are pretty much atomic in
nature. This means that a persistent communication channel between the browser and
the server is not directly supported. The protocol was intended as a means of requesting
and serving small files over the Net to produce fairly rich graphical content. The
lack of a persistent server-side state is obvious. This problem will be examined
later on in Chapter 16, "Advanced CGI/HTML."</P>
<P>Another problem lies in the fact that the HTML form is a static entity once it
reaches the browser. This means that it cannot handle certain user events that most
user interface programmers probably take for granted. For example, suppose you want
to enable or disable a field, depending on whether or not something is typed into
a text field. This is common practice for most graphical user interfaces, yet it
is not possible using HTML forms. There are alternatives for doing this kind of thing.
For example, Java provides this type of client-side executable capability. Embedding
a Java applet within your Web page can give you some more powerful capability. However,
this extra capability does not come free. You would have to learn how to program
with the Java AWT (applet window toolkit), and the transmission time for downloading
the applet can be higher.
<H3 ALIGN="CENTER"><A NAME="Heading33"></A><FONT COLOR="#000077">Summary</FONT></H3>
<P>Now that you have learned all about HTML forms and how they can be generated with
the <TT>CGI::Form</TT> module, you are ready to find out about the other, more powerful
aspect of this module: its capability to simplify the processing of the form.<BR>
<P ALIGN="CENTER"><A HREF="ch03.htm" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/ch03.htm"><IMG SRC="blanprev.gif" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/blanprev.gif" WIDTH="37" HEIGHT="37"
ALIGN="BOTTOM" BORDER="2"></A><A HREF="index-1.htm" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/index-1.htm"><IMG SRC="blantoc.gif" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/blantoc.gif" WIDTH="42"
HEIGHT="37" ALIGN="BOTTOM" BORDER="2"></A><A HREF="ch05.htm" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/ch05.htm"><IMG SRC="blannext.gif" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/blannext.gif"
WIDTH="45" HEIGHT="37" ALIGN="BOTTOM" BORDER="2"></A>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -