📄 ch19_12.htm
字号:
<HTML><HEAD><TITLE>Recipe 19.11. Creating Sticky Widgets (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:45:44Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch19_01.htm"TITLE="19. CGI Programming"><LINKREL="prev"HREF="ch19_11.htm"TITLE="19.10. Managing Cookies"><LINKREL="next"HREF="ch19_13.htm"TITLE="19.12. Writing a Multiscreen CGI Script"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_11.htm"TITLE="19.10. Managing Cookies"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 19.10. Managing Cookies"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch19_01.htm"TITLE="19. CGI Programming"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_13.htm"TITLE="19.12. Writing a Multiscreen CGI Script"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 19.12. Writing a Multiscreen CGI Script"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch19-13172">19.11. Creating Sticky Widgets</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-1230">Problem<ACLASS="indexterm"NAME="ch19-idx-1000005496-0"></A><ACLASS="indexterm"NAME="ch19-idx-1000005496-1"></A><ACLASS="indexterm"NAME="ch19-idx-1000005496-2"></A><ACLASS="indexterm"NAME="ch19-idx-1000005496-3"></A></A></H3><PCLASS="para">You want the default values for the fields in your form to be the last values submitted. For instance, you want to create a search form like AltaVista (<ACLASS="systemitem.url"HREF="http://altavista.digital.com/ ">http://altavista.digital.com/ </A>) where the keywords you're searching with appear in the search dialog above the results.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-1236">Solution</A></H3><PCLASS="para">Use CGI.pm's HTML shortcuts to create your form, which will automatically provide previous values as defaults:</P><PRECLASS="programlisting">print textfield("SEARCH"); # previous SEARCH value is the default</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-1000004904">Discussion</A></H3><PCLASS="para"><ACLASS="xref"HREF="ch19_12.htm#ch19-40191"TITLE="who.cgi">Example 19.8</A> is a simple script for querying the list of users currently logged in.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch19-40191">Example 19.8: who.cgi</A></H4><PRECLASS="programlisting">#!/usr/bin/perl -wT# <ACLASS="indexterm"NAME="ch19-idx-1000005502-0"></A>who.cgi - run who(1) on a user and format the results nicely$ENV{IFS}='';$ENV{PATH}='/bin:/usr/bin';use CGI qw(:standard);# print search formprint header(), start_html("Query Users"), h1("Search");print start_form(), p("Which user?", textfield("WHO")); submit(), end_form();# print results of the query if we have someone to look for$name = param("WHO");if ($name) { print h1("Results"); $html = ''; # call who and build up text of response foreach (`who`) { next unless /^$name\s/o; # only lines matching $name s/&/&amp;/g; # escape HTML s/</&lt;/g; s/>/&gt;/g; $html .= $_; } # nice message if we didn't find anyone by that name $html = $html || "$name is not logged in"; print pre($html);}print end_html();</PRE></DIV><PCLASS="para">The call to <CODECLASS="literal">textfield</CODE> generates HTML for a text entry field whose parameter name is <CODECLASS="literal">WHO</CODE>. After printing the form, we check whether we were called with a value for the <CODECLASS="literal">WHO</CODE> parameter. If so, we try to find the lines in the output from <EMCLASS="emphasis">who</EM> for that user. <ACLASS="indexterm"NAME="ch19-idx-1000005498-0"></A><ACLASS="indexterm"NAME="ch19-idx-1000005498-1"></A><ACLASS="indexterm"NAME="ch19-idx-1000005498-2"></A><ACLASS="indexterm"NAME="ch19-idx-1000005498-3"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-1318">See Also</A></H3><PCLASS="para">The documentation for the standard CGI module; <ACLASS="xref"HREF="ch19_05.htm"TITLE="Writing a Safe CGI Program">Recipe 19.4</A>; <ACLASS="xref"HREF="ch19_08.htm"TITLE="Formatting Lists and Tables with HTML Shortcuts">Recipe 19.7</A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_11.htm"TITLE="19.10. Managing Cookies"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 19.10. Managing Cookies"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_13.htm"TITLE="19.12. Writing a Multiscreen CGI Script"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 19.12. Writing a Multiscreen CGI Script"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">19.10. Managing Cookies</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">19.12. Writing a Multiscreen CGI Script</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -