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

📄 ch04.htm

📁 Web_Programming_with_Perl5,一个不错的Perl语言教程。
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<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 &lt;&lt;ENDOFMYHTML;



&lt;H3&gt;Important Information&lt;/H3&gt;



&lt;P&gt;This information is &lt;B&gt;very&lt;/B&gt; important! Everything up until the



&lt;BLINK&gt;ENDOFTEXT&lt;/BLINK&gt; line will appear in this page.



&lt;HR&gt;



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>&lt;/BODY&gt;</TT> and <TT>&lt;/HTML&gt;</TT> tags for you.</P>



<PRE><FONT COLOR="#0066FF">print $q-&gt;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>&lt;TABLE&gt;</TT>



and <TT>&lt;/TABLE&gt;</TT> tags. Rows within a table are defined using the <TT>&lt;TR&gt;</TT>



and <TT>&lt;/TR&gt;</TT> tags. Columns within the row are defined using the <TT>&lt;TD&gt;</TT>



and <TT>&lt;/TD&gt;</TT> tags. Header columns within the row are defined using the



<TT>&lt;TH&gt;</TT> and <TT>&lt;/TH&gt;</TT> tags. Let's take the restaurant questionnaire



form and make it look a bit nicer using a table. Although the <TT>&lt;TABLE&gt;</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 &lt;TABLE&gt; object.</FONT></H3>



<PRE><FONT COLOR="#0066FF">use CGI::Form;



$q = new CGI::Form;







print &quot;&lt;H1&gt;Customer Survey Using Tables&lt;/H1&gt;\n&quot;;



print $q-&gt;start_multipart_form();



print &quot;&lt;TABLE&gt;\n&lt;TR&gt;\n&lt;TD&gt;\n&quot;;



print &quot;What did you order? &quot;;



print &quot;&lt;/TD&gt;\n&lt;TD&gt;\n&quot;;



print $q-&gt;textfield( -name=&gt;'order', -value=&gt;&quot;&quot;, -maxlength=&gt;30, -size=&gt;30 );



print &quot;&lt;/TD&gt;\n&lt;/TR&gt;\n&lt;TR&gt;\n&lt;TD&gt;\n&quot;;



print &quot;How was the service? (5-good,1-bad) &quot;;



print &quot;&lt;/TD&gt;\n&lt;TD&gt;\n&quot;;



print $q-&gt;radio_group( -name=&gt;'serviceRating', -values=&gt;[`1','2','3','4','5'],



                      -default=&gt;'3' );



print &quot;&lt;/TD&gt;\n&lt;/TR&gt;\n&lt;TR&gt;\n&lt;TD&gt;\n&quot;;



print &quot;How did the food taste? (5-good,1-bad) &quot;;



print &quot;&lt;/TD&gt;\n&lt;TD&gt;\n&quot;;



print $q-&gt;radio_group( -name=&gt;'tasteRating', -values=&gt;[`1','2','3','4','5'],



                      -default=&gt;'3' );



print &quot;&lt;/TD&gt;\n&lt;/TR&gt;\n&lt;TR&gt;\n&lt;TD&gt;\n&quot;;



print &quot;What age group are you in? &quot;;



@ages=( `child', `teen', `20s', `30s', `40s', `50s', `senior' );



print &quot;&lt;/TD&gt;\n&lt;TD&gt;\n&quot;;



print $q-&gt;popup_menu( -name=&gt;'ageGroup', -values=&gt;\@ages );



print &quot;&lt;/TD&gt;\n&lt;/TR&gt;\n&lt;TR&gt;\n&lt;TD&gt;\n&quot;;



print $q-&gt;checkbox( -name=&gt;'comeAgain', -checked=&gt;'checked', -value=&gt;'yes',



                    -label=&gt;&quot;I would come back again &quot; );



print &quot;&lt;/TD&gt;\n&lt;/TR&gt;\n&lt;TR&gt;\n&lt;TD&gt;\n&quot;;



print $q-&gt;submit( -name=&gt;'Action', -value=&gt;'Submit' );



print $q-&gt;reset();



print &quot;&lt;/TABLE&gt;&quot;;



print $q-&gt;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>&lt;FRAME&gt;</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">&lt;HTML&gt;



&lt;FRAMESET ROWS=50%,50%&gt;



   &lt;FRAME SRC=&quot;http://vaders/cgi-bin/wpp/testform.cgi&quot;&gt;



   &lt;FRAME SRC=&quot;http://vaders/cgi-bin/wpp/tablform.cgi&quot;&gt;



&lt;/FRAMESET&gt;



&lt;/HTML&gt;



</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>&lt;FRAME&gt;</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 &lt;EMBED&gt; 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>&lt;EMBED&gt;</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>&lt;EMBED&gt;</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">&lt;embed src=movies/sample.avi width=300 height=100&gt;



&lt;br&gt;



&lt;embed src=pdfs/sample.pdf width=300 height=300&gt;



</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>&lt;EMBED&gt;</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>&lt;APPLET&gt;</TT>



tag, as follows:</P>



<PRE><FONT COLOR="#0066FF">&lt;APPLET CODE=JavaApplet.class WIDTH=200 HEIGHT=200&gt;



&lt;/APPLET&gt;



</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, &quot;Advanced CGI/HTML.&quot;</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 + -