.\" Automatically generated by Pod::Man 2.27 (Pod::Simple 3.28)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings. \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote. \*(C+ will
.\" give a nicer C++. Capital omega is used to do unbreakable dashes and
.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
. ds -- \(*W-
. ds PI pi
. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
. ds L" ""
. ds R" ""
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds -- \|\(em\|
. ds PI \(*p
. ds L" ``
. ds R" ''
. ds C`
. ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{
. if \nF \{
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. if !\nF==2 \{
. nr % 0
. nr F 2
. \}
. \}
.\}
.rr rF
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear. Run. Save yourself. No user-serviceable parts.
. \" fudge factors for nroff and troff
.if n \{\
. ds #H 0
. ds #V .8m
. ds #F .3m
. ds #[ \f1
. ds #] \fP
.\}
.if t \{\
. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
. ds #V .6m
. ds #F 0
. ds #[ \&
. ds #] \&
.\}
. \" simple accents for nroff and troff
.if n \{\
. ds ' \&
. ds ` \&
. ds ^ \&
. ds , \&
. ds ~ ~
. ds /
.\}
.if t \{\
. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
. \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
. \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
. \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
. ds : e
. ds 8 ss
. ds o a
. ds d- d\h'-1'\(ga
. ds D- D\h'-1'\(hy
. ds th \o'bp'
. ds Th \o'LP'
. ds ae ae
. ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "CGI::HTML::Functions 3"
.TH CGI::HTML::Functions 3 "2019-03-15" "perl v5.16.3" "User Contributed Perl Documentation"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
CGI::HTML::Functions \- Documentation for CGI.pm Legacy HTML Functionality
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
Nothing here \- please do not use this functionality, it is considered to
be legacy and essentially deprecated. This documentation exists solely to
aid in maintenance and migration of legacy code using this functionality
and you are strongly encouraged to migrate away from it. If you are working
on new code you should be using a template engine. For more information see
CGI::Alternatives.
.PP
If you really want to continue using the \s-1HTML\s0 generation functionality of \s-1CGI\s0.pm
then you should take a look at HTML::Tiny instead, which may give you a migration
path away from \s-1CGI\s0.pm's html generation functions; i strongly encourage you to
move towards template driven page generation for anything involving markup as
it will make porting your app to other frameworks much easier in the long run.
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
The documentation here should be considered an addendum to the sections in the
\&\s-1CGI\s0 documentation \- the sections here are named the same as those within the
\&\s-1CGI\s0 perldoc.
.SH "Calling CGI.pm routines"
.IX Header "Calling CGI.pm routines"
\&\s-1HTML\s0 tag functions have both attributes (the attribute=\*(L"value\*(R" pairs within the
tag itself) and contents (the part between the opening and closing pairs). To
distinguish between attributes and contents, \s-1CGI\s0.pm uses the convention of
passing \s-1HTML\s0 attributes as a hash reference as the first argument, and the
contents, if any, as any subsequent arguments. It works out like
this:
.PP
.Vb 6
\& Code Generated HTML
\& \-\-\-\- \-\-\-\-\-\-\-\-\-\-\-\-\-\-
\& h1() <h1 />
\& h1(\*(Aqsome\*(Aq,\*(Aqcontents\*(Aq); <h1>some contents</h1>
\& h1({\-align=>left}); <h1 align="LEFT">
\& h1({\-align=>left},\*(Aqcontents\*(Aq); <h1 align="LEFT">contents</h1>
.Ve
.PP
Many newcomers to \s-1CGI\s0.pm are puzzled by the difference between the calling
conventions for the \s-1HTML\s0 shortcuts, which require curly braces around the \s-1HTML\s0
tag attributes, and the calling conventions for other routines, which manage
to generate attributes without the curly brackets. Don't be confused. As a
convenience the curly braces are optional in all but the \s-1HTML\s0 shortcuts. If you
like, you can use curly braces when calling any routine that takes named
arguments. For example:
.PP
.Vb 1
\& print $q\->header( { \-type => \*(Aqimage/gif\*(Aq, \-expires => \*(Aq+3d\*(Aq } );
.Ve
.PP
If you use warnings, you will be warned that some \s-1CGI\s0.pm argument names
conflict with built-in perl functions. The most frequent of these is the
\&\-values argument, used to create multi-valued menus, radio button clusters
and the like. To get around this warning, you have several choices:
.IP "1." 4
Use another name for the argument, if one is available.
For example, \-value is an alias for \-values.
.IP "2." 4
Change the capitalization, e.g. \-Values
.IP "3." 4
Put quotes around the argument name, e.g. '\-values'
.SS "Function-oriented interface \s-1HTML\s0 exports"
.IX Subsection "Function-oriented interface HTML exports"
Here is a list of the \s-1HTML\s0 related function sets you can import:
.IP "\fB:form\fR" 4
.IX Item ":form"
Import all fill-out form generating methods, such as \fB\f(BItextfield()\fB\fR.
.IP "\fB:html2\fR" 4
.IX Item ":html2"
Import all methods that generate \s-1HTML 2.0\s0 standard elements.
.IP "\fB:html3\fR" 4
.IX Item ":html3"
Import all methods that generate \s-1HTML 3.0\s0 elements (such as
<table>, <super> and <sub>).
.IP "\fB:html4\fR" 4
.IX Item ":html4"
Import all methods that generate \s-1HTML 4\s0 elements (such as
<abbrev>, <acronym> and <thead>).
.IP "\fB:netscape\fR" 4
.IX Item ":netscape"
Import the <blink>, <fontsize> and <center> tags.
.IP "\fB:html\fR" 4
.IX Item ":html"
Import all HTML-generating shortcuts (i.e. 'html2', 'html3', 'html4' and 'netscape')
.IP "\fB:standard\fR" 4
.IX Item ":standard"
Import \*(L"standard\*(R" features, 'html2', 'html3', 'html4', 'ssl', 'form' and 'cgi'.
.PP
If you import any of the state-maintaining \s-1CGI\s0 or form-generating methods,
a default \s-1CGI\s0 object will be created and initialized automatically the first
time you use any of the methods that require one to be present. This includes
\&\fB\f(BIparam()\fB\fR, \fB\f(BItextfield()\fB\fR, \fB\f(BIsubmit()\fB\fR and the like. (If you need direct access
to the \s-1CGI\s0 object, you can find it in the global variable \fB\f(CB$CGI::Q\fB\fR).
.SS "Pragmas"
.IX Subsection "Pragmas"
Additional \s-1HTML\s0 generation related pragms:
.IP "\-nosticky" 4
.IX Item "-nosticky"
By default the \s-1CGI\s0 module implements a state-preserving behavior called
\&\*(L"sticky\*(R" fields. The way this works is that if you are regenerating a form,
the methods that generate the form field values will interrogate \fIparam()\fR
to see if similarly-named parameters are present in the query string. If
they find a like-named parameter, they will use it to set their default values.
.Sp
Sometimes this isn't what you want. The \fB\-nosticky\fR pragma prevents this
behavior. You can also selectively change the sticky behavior in each element
that you generate.
.IP "\-tabindex" 4
.IX Item "-tabindex"
Automatically add tab index attributes to each form field. With this option
turned off, you can still add tab indexes manually by passing a \-tabindex
option to each field-generating method.
.IP "\-no_xhtml" 4
.IX Item "-no_xhtml"
By default, \s-1CGI\s0.pm versions 2.69 and higher emit \s-1XHTML
\&\s0(http://www.w3.org/TR/xhtml1/). The \-no_xhtml pragma disables this feature.
Thanks to Michalis Kabrianis <kabrianis@hellug.gr> for this feature.
.Sp
If \fIstart_html()\fR's \-dtd parameter specifies an \s-1HTML 2.0, 3.2, 4.0\s0 or 4.01 \s-1DTD,
XHTML\s0 will automatically be disabled without needing to use this pragma.
.SS "Special forms for importing HTML-tag functions"
.IX Subsection "Special forms for importing HTML-tag functions"
Many of the methods generate \s-1HTML\s0 tags. As described below, tag functions
automatically generate both the opening and closing tags. For example:
.PP
.Vb 1
\& print h1(\*(AqLevel 1 Header\*(Aq);
.Ve
.PP
produces
.PP
.Vb 1
\& <h1>Level 1 Header</h1>
.Ve
.PP
There will be some times when you want to produce the start and end tags
yourself. In this case, you can use the form start_\fItag_name\fR and
end_\fItag_name\fR, as in:
.PP
.Vb 1
\& print start_h1,\*(AqLevel 1 Header\*(Aq,end_h1;
.Ve
.SS "Creating the \s-1HTML\s0 document header"
.IX Subsection "Creating the HTML document header"
.Vb 10
\& print start_html(
\& \-title => \*(AqSecrets of the Pyramids\*(Aq,
\& \-author => \*(Aqfred@capricorn.org\*(Aq,
\& \-base => \*(Aqtrue\*(Aq,
\& \-target => \*(Aq_blank\*(Aq,
\& \-meta => {\*(Aqkeywords\*(Aq=>\*(Aqpharaoh secret mummy\*(Aq,
\& \*(Aqcopyright\*(Aq => \*(Aqcopyright 1996 King Tut\*(Aq},
\& \-style => {\*(Aqsrc\*(Aq=>\*(Aq/styles/style1.css\*(Aq},
\& \-BGCOLOR => \*(Aqblue\*(Aq
\& );
.Ve
.PP
The \fIstart_html()\fR routine creates the top of the page, along with a lot of
optional information that controls the page's appearance and behavior.
.PP
This method returns a canned \s-1HTML\s0 header and the opening <body> tag. All
parameters are optional. In the named parameter form, recognized parameters
are \-title, \-author, \-base, \-xbase, \-dtd, \-lang and \-target (see below for the
explanation). Any additional parameters you provide, such as the unofficial
\&\s-1BGCOLOR\s0 attribute, are added to the <body> tag. Additional parameters must be
proceeded by a hyphen.
.PP
The argument \fB\-xbase\fR allows you to provide an \s-1HREF\s0 for the <base> tag different
from the current location, as in
.PP
.Vb 1
\& \-xbase => "http://home.mcom.com/"
.Ve
.PP
All relative links will be interpreted relative to this tag.
.PP
The argument \fB\-target\fR allows you to provide a default target frame for all the
links and fill-out forms on the page. \fBThis is a non-standard \s-1HTTP\s0 feature\fR
\&\fBwhich only works with some browsers!\fR
.PP
.Vb 1
\& \-target => "answer_window"
.Ve
.PP
All relative links will be interpreted relative to this tag. You add arbitrary
meta information to the header with the \fB\-meta\fR argument. This argument expects
a reference to a hash containing name/value pairs of meta information. These will
be turned into a series of header <meta> tags that look something like this:
.PP
.Vb 2
\& <meta name="keywords" content="pharaoh secret mummy">
\& <meta name="description" content="copyright 1996 King Tut">
.Ve
.PP
To create an HTTP-EQUIV type of <meta> tag, use \fB\-head\fR, described below.
.PP
The \fB\-style\fR argument is used to incorporate cascading stylesheets into your
code. See the section on \s-1CASCADING STYLESHEETS\s0 for more information.
.PP
The \fB\-lang\fR argument is used to incorporate a language attribute into the <html>
tag. For example:
.PP
.Vb 1
\& print $q\->start_html( \-lang => \*(Aqfr\-CA\*(Aq );
.Ve
.PP
The default if not specified is \*(L"en-US\*(R" for \s-1US\s0 English, unless the \-dtd parameter
specifies an \s-1HTML 2.0\s0 or 3.2 \s-1DTD,\s0 in which case the lang attribute is left off.
You can force the lang attribute to left off in other cases by passing an empty
string (\-lang=>'').
.PP
The \fB\-encoding\fR argument can be used to specify the character set for \s-1XHTML.\s0 It
defaults to iso\-8859\-1 if not specified.
.PP
The \fB\-dtd\fR argument can be used to specify a public \s-1DTD\s0 identifier string. For
example:
.PP
.Vb 1
\& \-dtd => \*(Aq\-//W3C//DTD HTML 4.01 Transitional//EN\*(Aq)
.Ve
.PP
Alternatively, it can take public and system \s-1DTD\s0 identifiers as an array:
.PP
.Vb 4
\& \-dtd => [
\& \*(Aq\-//W3C//DTD HTML 4.01 Transitional//EN\*(Aq,
\& \*(Aqhttp://www.w3.org/TR/html4/loose.dtd\*(Aq
\& ]
.Ve
.PP
For the public \s-1DTD\s0 identifier to be considered, it must be valid. Otherwise it
will be replaced by the default \s-1DTD.\s0 If the public \s-1DTD\s0 contains '\s-1XHTML\s0', \s-1CGI\s0.pm
will emit \s-1XML.\s0
.PP
The \fB\-declare_xml\fR argument, when used in conjunction with \s-1XHTML,\s0 will put a
<?xml> declaration at the top of the \s-1HTML\s0 header. The sole purpose of this
declaration is to declare the character set encoding. In the absence of
\&\-declare_xml, the output \s-1HTML\s0 will contain a <meta> tag that specifies the
encoding, allowing the \s-1HTML\s0 to pass most validators. The default for \-declare_xml
is false.
.PP
You can place other arbitrary \s-1HTML\s0 elements to the <head> section with the
\&\fB\-head\fR tag. For example, to place a <link> element in the head section, use
this:
.PP
.Vb 6
\& print start_html(
\& \-head => Link({
\& \-rel => \*(Aqshortcut icon\*(Aq,
\& \-href => \*(Aqfavicon.ico\*(Aq
\& })
\& );
.Ve
.PP
To incorporate multiple \s-1HTML\s0 elements into the <head> section, just pass an
array reference:
.PP
.Vb 12
\& print start_html(
\& \-head => [
\& Link({
\& \-rel => \*(Aqnext\*(Aq,
\& \-href => \*(Aqhttp://www.capricorn.com/s2.html\*(Aq
\& }),
\& Link({
\& \-rel => \*(Aqprevious\*(Aq,
\& \-href => \*(Aqhttp://www.capricorn.com/s1.html\*(Aq
\& })
\& ]
\& );
.Ve
.PP
And here's how to create an HTTP-EQUIV <meta> tag:
.PP
.Vb 6
\& print start_html(
\& \-head => meta({
\& \-http_equiv => \*(AqContent\-Type\*(Aq,
\& \-content => \*(Aqtext/html\*(Aq
\& })
\& );
.Ve
.PP
\&\s-1JAVASCRIPTING:\s0 The \fB\-script\fR, \fB\-noScript\fR, \fB\-onLoad\fR, \fB\-onMouseOver\fR,
\&\fB\-onMouseOut\fR and \fB\-onUnload\fR parameters are used to add JavaScript calls to
your pages. \fB\-script\fR should point to a block of text containing JavaScript
function definitions. This block will be placed within a <script> block inside
the \s-1HTML \s0(not \s-1HTTP\s0) header. The block is placed in the header in order to give
your page a fighting chance of having all its JavaScript functions in place even
if the user presses the stop button before the page has loaded completely. \s-1CGI\s0.pm
attempts to format the script in such a way that JavaScript-naive browsers will
not choke on the code: unfortunately there are some browsers that get confused by
it nevertheless.
.PP
The \fB\-onLoad\fR and \fB\-onUnload\fR parameters point to fragments of JavaScript code
to execute when the page is respectively opened and closed by the browser.
Usually these parameters are calls to functions defined in the \fB\-script\fR field:
.PP
.Vb 10
\& $q = CGI\->new;
\& print header;
\& $JSCRIPT = <<END;
\& // Ask a silly question
\& function riddle_me_this() {
\& var r = prompt(
\& "What walks on four legs in the morning, " +
\& "two legs in the afternoon, " +
\& "and three legs in the evening?"
\& );
\& response(r);
\& }
\& // Get a silly answer
\& function response(answer) {
\& if (answer == "man")
\& alert("Right you are!");
\& else
\& alert("Wrong! Guess again.");
\& }
\& END
\& print start_html(
\& \-title => \*(AqThe Riddle of the Sphinx\*(Aq,
\& \-script => $JSCRIPT
\& );
.Ve
.PP
Use the \fB\-noScript\fR parameter to pass some \s-1HTML\s0 text that will be displayed on
browsers that do not have JavaScript (or browsers where JavaScript is turned
off).
.PP
The <script> tag, has several attributes including \*(L"type\*(R", \*(L"charset\*(R" and \*(L"src\*(R".
\&\*(L"src\*(R" allows you to keep JavaScript code in an external file. To use these
attributes pass a \s-1HASH\s0 reference in the \fB\-script\fR parameter containing one or
more of \-type, \-src, or \-code:
.PP
.Vb 6
\& print $q\->start_html(
\& \-title => \*(AqThe Riddle of the Sphinx\*(Aq,
\& \-script => {
\& \-type => \*(AqJAVASCRIPT\*(Aq,
\& \-src => \*(Aq/javascript/sphinx.js\*(Aq}
\& );
\&
\& print $q\->(
\& \-title => \*(AqThe Riddle of the Sphinx\*(Aq,
\& \-script => {
\& \-type => \*(AqPERLSCRIPT\*(Aq,
\& \-code => \*(Aqprint "hello world!\en;"\*(Aq
\& }
\& );
.Ve
.PP
A final feature allows you to incorporate multiple <script> sections into the
header. Just pass the list of script sections as an array reference. This allows
you to specify different source files for different dialects of JavaScript.
Example:
.PP
.Vb 10
\& print $q\->start_html(
\& \-title => \*(AqThe Riddle of the Sphinx\*(Aq,
\& \-script => [
\& {
\& \-type => \*(Aqtext/javascript\*(Aq,
\& \-src => \*(Aq/javascript/utilities10.js\*(Aq
\& },
\& {
\& \-type => \*(Aqtext/javascript\*(Aq,
\& \-src => \*(Aq/javascript/utilities11.js\*(Aq
\& },
\& {
\& \-type => \*(Aqtext/jscript\*(Aq,
\& \-src => \*(Aq/javascript/utilities12.js\*(Aq
\& },
\& {
\& \-type => \*(Aqtext/ecmascript\*(Aq,
\& \-src => \*(Aq/javascript/utilities219.js\*(Aq
\& }
\& ]
\& );
.Ve
.PP
The option \*(L"\-language\*(R" is a synonym for \-type, and is supported for backwards
compatibility.
.PP
The old-style positional parameters are as follows:
.PP
\&\fBParameters:\fR
.IP "1." 4
The title
.IP "2." 4
The author's e\-mail address (will create a <link rev=\*(L"\s-1MADE\*(R"\s0> tag if present
.IP "3." 4
A 'true' flag if you want to include a <base> tag in the header. This helps
resolve relative addresses to absolute ones when the document is moved, but
makes the document hierarchy non-portable. Use with care!
.PP
Other parameters you want to include in the <body> tag may be appended to these.
This is a good place to put \s-1HTML\s0 extensions, such as colors and wallpaper
patterns.
.SS "Ending the Html document:"
.IX Subsection "Ending the Html document:"
.Vb 1
\& print $q\->end_html;
.Ve
.PP
This ends an \s-1HTML\s0 document by printing the </body></html> tags.
.SH "CREATING STANDARD HTML ELEMENTS:"
.IX Header "CREATING STANDARD HTML ELEMENTS:"
\&\s-1CGI\s0.pm defines general \s-1HTML\s0 shortcut methods for many \s-1HTML\s0 tags. \s-1HTML\s0 shortcuts are named after a single
\&\s-1HTML\s0 element and return a fragment of \s-1HTML\s0 text. Example:
.PP
.Vb 7
\& print $q\->blockquote(
\& "Many years ago on the island of",
\& $q\->a({href=>"http://crete.org/"},"Crete"),
\& "there lived a Minotaur named",
\& $q\->strong("Fred."),
\& ),
\& $q\->hr;
.Ve
.PP
This results in the following \s-1HTML\s0 code (extra newlines have been
added for readability):
.PP
.Vb 6
\& <blockquote>
\& Many years ago on the island of
\& <a href="http://crete.org/">Crete</a> there lived
\& a minotaur named <strong>Fred.</strong>
\& </blockquote>
\& <hr>
.Ve
.PP
If you find the syntax for calling the \s-1HTML\s0 shortcuts awkward, you can
import them into your namespace and dispense with the object syntax
completely (see the next section for more details):
.PP
.Vb 8
\& use CGI \*(Aq:standard\*(Aq;
\& print blockquote(
\& "Many years ago on the island of",
\& a({href=>"http://crete.org/"},"Crete"),
\& "there lived a minotaur named",
\& strong("Fred."),
\& ),
\& hr;
.Ve
.SS "Providing arguments to \s-1HTML\s0 shortcuts"
.IX Subsection "Providing arguments to HTML shortcuts"
The \s-1HTML\s0 methods will accept zero, one or multiple arguments. If you
provide no arguments, you get a single tag:
.PP
.Vb 1
\& print hr; # <hr>
.Ve
.PP
If you provide one or more string arguments, they are concatenated
together with spaces and placed between opening and closing tags:
.PP
.Vb 1
\& print h1("Chapter","1"); # <h1>Chapter 1</h1>"
.Ve
.PP
If the first argument is a hash reference, then the keys
and values of the hash become the \s-1HTML\s0 tag's attributes:
.PP
.Vb 2
\& print a({\-href=>\*(Aqfred.html\*(Aq,\-target=>\*(Aq_new\*(Aq},
\& "Open a new frame");
\&
\& <a href="fred.html",target="_new">Open a new frame</a>
.Ve
.PP
You may dispense with the dashes in front of the attribute names if
you prefer:
.PP
.Vb 1
\& print img {src=>\*(Aqfred.gif\*(Aq,align=>\*(AqLEFT\*(Aq};
\&
\& <img align="LEFT" src="fred.gif">
.Ve
.PP
Sometimes an \s-1HTML\s0 tag attribute has no argument. For example, ordered
lists can be marked as \s-1COMPACT. \s0 The syntax for this is an argument that
that points to an undef string:
.PP
.Vb 1
\& print ol({compact=>undef},li(\*(Aqone\*(Aq),li(\*(Aqtwo\*(Aq),li(\*(Aqthree\*(Aq));
.Ve
.PP
Prior to \s-1CGI\s0.pm version 2.41, providing an empty ('') string as an
attribute argument was the same as providing undef. However, this has
changed in order to accommodate those who want to create tags of the form
<img alt="">. The difference is shown in these two pieces of code:
.PP
.Vb 3
\& CODE RESULT
\& img({alt=>undef}) <img alt>
\& img({alt=>\*(Aq\*(Aq}) <img alt="">
.Ve
.SS "The distributive property of \s-1HTML\s0 shortcuts"
.IX Subsection "The distributive property of HTML shortcuts"
One of the cool features of the \s-1HTML\s0 shortcuts is that they are
distributive. If you give them an argument consisting of a
\&\fBreference\fR to a list, the tag will be distributed across each
element of the list. For example, here's one way to make an ordered
list:
.PP
.Vb 3
\& print ul(
\& li({\-type=>\*(Aqdisc\*(Aq},[\*(AqSneezy\*(Aq,\*(AqDoc\*(Aq,\*(AqSleepy\*(Aq,\*(AqHappy\*(Aq])
\& );
.Ve
.PP
This example will result in \s-1HTML\s0 output that looks like this:
.PP
.Vb 6
\& <ul>
\& <li type="disc">Sneezy</li>
\& <li type="disc">Doc</li>
\& <li type="disc">Sleepy</li>
\& <li type="disc">Happy</li>
\& </ul>
.Ve
.PP
This is extremely useful for creating tables. For example:
.PP
.Vb 11
\& print table({\-border=>undef},
\& caption(\*(AqWhen Should You Eat Your Vegetables?\*(Aq),
\& Tr({\-align=>\*(AqCENTER\*(Aq,\-valign=>\*(AqTOP\*(Aq},
\& [
\& th([\*(AqVegetable\*(Aq, \*(AqBreakfast\*(Aq,\*(AqLunch\*(Aq,\*(AqDinner\*(Aq]),
\& td([\*(AqTomatoes\*(Aq , \*(Aqno\*(Aq, \*(Aqyes\*(Aq, \*(Aqyes\*(Aq]),
\& td([\*(AqBroccoli\*(Aq , \*(Aqno\*(Aq, \*(Aqno\*(Aq, \*(Aqyes\*(Aq]),
\& td([\*(AqOnions\*(Aq , \*(Aqyes\*(Aq,\*(Aqyes\*(Aq, \*(Aqyes\*(Aq])
\& ]
\& )
\& );
.Ve
.SS "\s-1HTML\s0 shortcuts and list interpolation"
.IX Subsection "HTML shortcuts and list interpolation"
Consider this bit of code:
.PP
.Vb 1
\& print blockquote(em(\*(AqHi\*(Aq),\*(Aqmom!\*(Aq));
.Ve
.PP
It will ordinarily return the string that you probably expect, namely:
.PP
.Vb 1
\& <blockquote><em>Hi</em> mom!</blockquote>
.Ve
.PP
Note the space between the element \*(L"Hi\*(R" and the element \*(L"mom!\*(R".
\&\s-1CGI\s0.pm puts the extra space there using array interpolation, which is
controlled by the magic $\*(L" variable. Sometimes this extra space is
not what you want, for example, when you are trying to align a series
of images. In this case, you can simply change the value of $\*(R" to an
empty string.
.PP
.Vb 4
\& {
\& local($") = \*(Aq\*(Aq;
\& print blockquote(em(\*(AqHi\*(Aq),\*(Aqmom!\*(Aq));
\& }
.Ve
.PP
I suggest you put the code in a block as shown here. Otherwise the
change to $" will affect all subsequent code until you explicitly
reset it.
.SS "Non-standard \s-1HTML\s0 shortcuts"
.IX Subsection "Non-standard HTML shortcuts"
A few \s-1HTML\s0 tags don't follow the standard pattern for various
reasons.
.PP
\&\fB\f(BIcomment()\fB\fR generates an \s-1HTML\s0 comment (<!\-\- comment \-\->). Call it
like
.PP
.Vb 1
\& print comment(\*(Aqhere is my comment\*(Aq);
.Ve
.PP
Because of conflicts with built-in perl functions, the following functions
begin with initial caps:
.PP
.Vb 6
\& Select
\& Tr
\& Link
\& Delete
\& Accept
\& Sub
.Ve
.PP
In addition, \fIstart_html()\fR, \fIend_html()\fR, \fIstart_form()\fR, \fIend_form()\fR,
\&\fIstart_multipart_form()\fR and all the fill-out form tags are special.
See their respective sections.
.SS "Autoescaping \s-1HTML\s0"
.IX Subsection "Autoescaping HTML"
By default, all \s-1HTML\s0 that is emitted by the form-generating functions
is passed through a function called \fIescapeHTML()\fR:
.ie n .IP "$escaped_string = escapeHTML(""unescaped string"");" 4
.el .IP "\f(CW$escaped_string\fR = escapeHTML(``unescaped string'');" 4
.IX Item "$escaped_string = escapeHTML(unescaped string);"
Escape \s-1HTML\s0 formatting characters in a string. Internally this calls
HTML::Entities (encode_entities) so really you should just use that
instead \- the default list of chars that will be encoded (passed to the
HTML::Entities encode_entities method) is:
.Sp
.Vb 1
\& & < > " \ex8b \ex9b \*(Aq
.Ve
.Sp
you can control this list by setting the value of \f(CW$CGI::ENCODE_ENTITIES:\fR
.Sp
.Vb 2
\& # only encode < >
\& $CGI::ENCODE_ENTITIES = q{<>}
.Ve
.Sp
if you want to encode \fBall\fR entities then undef \f(CW$CGI::ENCODE_ENTITIES:\fR
.Sp
.Vb 2
\& # encode all entities
\& $CGI::ENCODE_ENTITIES = undef;
.Ve
.PP
The automatic escaping does not apply to other shortcuts, such as
\&\fIh1()\fR. You should call \fIescapeHTML()\fR yourself on untrusted data in
order to protect your pages against nasty tricks that people may enter
into guestbooks, etc.. To change the character set, use \fIcharset()\fR.
To turn autoescaping off completely, use \fIautoEscape\fR\|(0):
.ie n .IP "$charset = charset([$charset]);" 4
.el .IP "\f(CW$charset\fR = charset([$charset]);" 4
.IX Item "$charset = charset([$charset]);"
Get or set the current character set.
.ie n .IP "$flag = autoEscape([$flag]);" 4
.el .IP "\f(CW$flag\fR = autoEscape([$flag]);" 4
.IX Item "$flag = autoEscape([$flag]);"
Get or set the value of the autoescape flag.
.SH "CREATING FILL-OUT FORMS:"
.IX Header "CREATING FILL-OUT FORMS:"
\&\fIGeneral note\fR The various form-creating methods all return strings
to the caller, containing the tag or tags that will create the requested
form element. You are responsible for actually printing out these strings.
It's set up this way so that you can place formatting tags
around the form elements.
.PP
\&\fIAnother note\fR The default values that you specify for the forms are only
used the \fBfirst\fR time the script is invoked (when there is no query
string). On subsequent invocations of the script (when there is a query
string), the former values are used even if they are blank.
.PP
If you want to change the value of a field from its previous value, you have two
choices:
.PP
(1) call the \fIparam()\fR method to set it.
.PP
(2) use the \-override (alias \-force) parameter (a new feature in version 2.15).
This forces the default value to be used, regardless of the previous value:
.PP
.Vb 5
\& print textfield(\-name=>\*(Aqfield_name\*(Aq,
\& \-default=>\*(Aqstarting value\*(Aq,
\& \-override=>1,
\& \-size=>50,
\& \-maxlength=>80);
.Ve
.PP
\&\fIYet another note\fR By default, the text and labels of form elements are
escaped according to \s-1HTML\s0 rules. This means that you can safely use
\&\*(L"<\s-1CLICK ME\s0>\*(R" as the label for a button. However, it also interferes with
your ability to incorporate special \s-1HTML\s0 character sequences, such as Á,
into your fields. If you wish to turn off automatic escaping, call the
\&\fIautoEscape()\fR method with a false value immediately after creating the \s-1CGI\s0 object:
.PP
.Vb 2
\& $q = CGI\->new;
\& $q\->autoEscape(0);
.Ve
.PP
Note that \fIautoEscape()\fR is exclusively used to effect the behavior of how some
\&\s-1CGI\s0.pm \s-1HTML\s0 generation functions handle escaping. Calling \fIescapeHTML()\fR
explicitly will always escape the \s-1HTML.\s0
.PP
\&\fIA Lurking Trap!\fR Some of the form-element generating methods return
multiple tags. In a scalar context, the tags will be concatenated
together with spaces, or whatever is the current value of the $"
global. In a list context, the methods will return a list of
elements, allowing you to modify them if you wish. Usually you will
not notice this behavior, but beware of this:
.PP
.Vb 1
\& printf("%s\en",end_form())
.Ve
.PP
\&\fIend_form()\fR produces several tags, and only the first of them will be
printed because the format only expects one value.
.PP
<p>
.SS "Creating an isindex tag"
.IX Subsection "Creating an isindex tag"
.Vb 1
\& print isindex(\-action=>$action);
\&
\& \-or\-
\&
\& print isindex($action);
.Ve
.PP
Prints out an <isindex> tag. Not very exciting. The parameter
\&\-action specifies the \s-1URL\s0 of the script to process the query. The
default is to process the query with the current script.
.SS "Starting and ending a form"
.IX Subsection "Starting and ending a form"
.Vb 5
\& print start_form(\-method=>$method,
\& \-action=>$action,
\& \-enctype=>$encoding);
\& <... various form stuff ...>
\& print end_form;
\&
\& \-or\-
\&
\& print start_form($method,$action,$encoding);
\& <... various form stuff ...>
\& print end_form;
.Ve
.PP
\&\fIstart_form()\fR will return a <form> tag with the optional method,
action and form encoding that you specify. The defaults are:
.PP
.Vb 4
\& method: POST
\& action: this script
\& enctype: application/x\-www\-form\-urlencoded for non\-XHTML
\& multipart/form\-data for XHTML, see multipart/form\-data below.
.Ve
.PP
\&\fIend_form()\fR returns the closing </form> tag.
.PP
\&\fIstart_form()\fR's enctype argument tells the browser how to package the various
fields of the form before sending the form to the server. Two
values are possible:
.IP "\fBapplication/x\-www\-form\-urlencoded\fR" 4
.IX Item "application/x-www-form-urlencoded"
This is the older type of encoding. It is compatible with many \s-1CGI\s0 scripts and is
suitable for short fields containing text data. For your
convenience, \s-1CGI\s0.pm stores the name of this encoding
type in \fB&CGI::URL_ENCODED\fR.
.IP "\fBmultipart/form\-data\fR" 4
.IX Item "multipart/form-data"
This is the newer type of encoding.
It is suitable for forms that contain very large fields or that
are intended for transferring binary data. Most importantly,
it enables the \*(L"file upload\*(R" feature. For
your convenience, \s-1CGI\s0.pm stores the name of this encoding type
in \fB&CGI::MULTIPART\fR
.Sp
Forms that use this type of encoding are not easily interpreted
by \s-1CGI\s0 scripts unless they use \s-1CGI\s0.pm or another library designed
to handle them.
.Sp
If \s-1XHTML\s0 is activated (the default), then forms will be automatically
created using this type of encoding.
.PP
The \fIstart_form()\fR method uses the older form of encoding by
default unless \s-1XHTML\s0 is requested. If you want to use the
newer form of encoding by default, you can call
\&\fB\f(BIstart_multipart_form()\fB\fR instead of \fB\f(BIstart_form()\fB\fR. The
method \fB\f(BIend_multipart_form()\fB\fR is an alias to \fB\f(BIend_form()\fB\fR.
.PP
\&\s-1JAVASCRIPTING:\s0 The \fB\-name\fR and \fB\-onSubmit\fR parameters are provided
for use with JavaScript. The \-name parameter gives the
form a name so that it can be identified and manipulated by
JavaScript functions. \-onSubmit should point to a JavaScript
function that will be executed just before the form is submitted to your
server. You can use this opportunity to check the contents of the form
for consistency and completeness. If you find something wrong, you
can put up an alert box or maybe fix things up yourself. You can
abort the submission by returning false from this function.
.PP
Usually the bulk of JavaScript functions are defined in a <script>
block in the \s-1HTML\s0 header and \-onSubmit points to one of these function
call. See \fIstart_html()\fR for details.
.SS "Form elements"
.IX Subsection "Form elements"
After starting a form, you will typically create one or more
textfields, popup menus, radio groups and other form elements. Each
of these elements takes a standard set of named arguments. Some
elements also have optional arguments. The standard arguments are as
follows:
.IP "\fB\-name\fR" 4
.IX Item "-name"
The name of the field. After submission this name can be used to
retrieve the field's value using the \fIparam()\fR method.
.IP "\fB\-value\fR, \fB\-values\fR" 4
.IX Item "-value, -values"
The initial value of the field which will be returned to the script
after form submission. Some form elements, such as text fields, take
a single scalar \-value argument. Others, such as popup menus, take a
reference to an array of values. The two arguments are synonyms.
.IP "\fB\-tabindex\fR" 4
.IX Item "-tabindex"
A numeric value that sets the order in which the form element receives
focus when the user presses the tab key. Elements with lower values
receive focus first.
.IP "\fB\-id\fR" 4
.IX Item "-id"
A string identifier that can be used to identify this element to
JavaScript and \s-1DHTML.\s0
.IP "\fB\-override\fR" 4
.IX Item "-override"
A boolean, which, if true, forces the element to take on the value
specified by \fB\-value\fR, overriding the sticky behavior described
earlier for the \fB\-nosticky\fR pragma.
.IP "\fB\-onChange\fR, \fB\-onFocus\fR, \fB\-onBlur\fR, \fB\-onMouseOver\fR, \fB\-onMouseOut\fR, \fB\-onSelect\fR" 4
.IX Item "-onChange, -onFocus, -onBlur, -onMouseOver, -onMouseOut, -onSelect"
These are used to assign JavaScript event handlers. See the
JavaScripting section for more details.
.PP
Other common arguments are described in the next section. In addition
to these, all attributes described in the \s-1HTML\s0 specifications are
supported.
.SS "Creating a text field"
.IX Subsection "Creating a text field"
.Vb 5
\& print textfield(\-name=>\*(Aqfield_name\*(Aq,
\& \-value=>\*(Aqstarting value\*(Aq,
\& \-size=>50,
\& \-maxlength=>80);
\& \-or\-
\&
\& print textfield(\*(Aqfield_name\*(Aq,\*(Aqstarting value\*(Aq,50,80);
.Ve
.PP
\&\fItextfield()\fR will return a text input field.
.PP
\&\fBParameters\fR
.IP "1." 4
The first parameter is the required name for the field (\-name).
.IP "2." 4
The optional second parameter is the default starting value for the field
contents (\-value, formerly known as \-default).
.IP "3." 4
The optional third parameter is the size of the field in
characters (\-size).
.IP "4." 4
The optional fourth parameter is the maximum number of characters the
field will accept (\-maxlength).
.PP
As with all these methods, the field will be initialized with its
previous contents from earlier invocations of the script.
When the form is processed, the value of the text field can be
retrieved with:
.PP
.Vb 1
\& $value = param(\*(Aqfoo\*(Aq);
.Ve
.PP
If you want to reset it from its initial value after the script has been
called once, you can do so like this:
.PP
.Vb 1
\& param(\*(Aqfoo\*(Aq,"I\*(Aqm taking over this value!");
.Ve
.SS "Creating a big text field"
.IX Subsection "Creating a big text field"
.Vb 4
\& print textarea(\-name=>\*(Aqfoo\*(Aq,
\& \-default=>\*(Aqstarting value\*(Aq,
\& \-rows=>10,
\& \-columns=>50);
\&
\& \-or
\&
\& print textarea(\*(Aqfoo\*(Aq,\*(Aqstarting value\*(Aq,10,50);
.Ve
.PP
\&\fItextarea()\fR is just like textfield, but it allows you to specify
rows and columns for a multiline text entry box. You can provide
a starting value for the field, which can be long and contain
multiple lines.
.SS "Creating a password field"
.IX Subsection "Creating a password field"
.Vb 5
\& print password_field(\-name=>\*(Aqsecret\*(Aq,
\& \-value=>\*(Aqstarting value\*(Aq,
\& \-size=>50,
\& \-maxlength=>80);
\& \-or\-
\&
\& print password_field(\*(Aqsecret\*(Aq,\*(Aqstarting value\*(Aq,50,80);
.Ve
.PP
\&\fIpassword_field()\fR is identical to \fItextfield()\fR, except that its contents
will be starred out on the web page.
.SS "Creating a file upload field"
.IX Subsection "Creating a file upload field"
.Vb 5
\& print filefield(\-name=>\*(Aquploaded_file\*(Aq,
\& \-default=>\*(Aqstarting value\*(Aq,
\& \-size=>50,
\& \-maxlength=>80);
\& \-or\-
\&
\& print filefield(\*(Aquploaded_file\*(Aq,\*(Aqstarting value\*(Aq,50,80);
.Ve
.PP
\&\fIfilefield()\fR will return a file upload field.
In order to take full advantage of this \fIyou must use the new
multipart encoding scheme\fR for the form. You can do this either
by calling \fB\f(BIstart_form()\fB\fR with an encoding type of \fB&CGI::MULTIPART\fR,
or by calling the new method \fB\f(BIstart_multipart_form()\fB\fR instead of
vanilla \fB\f(BIstart_form()\fB\fR.
.PP
\&\fBParameters\fR
.IP "1." 4
The first parameter is the required name for the field (\-name).
.IP "2." 4
The optional second parameter is the starting value for the field contents
to be used as the default file name (\-default).
.Sp
For security reasons, browsers don't pay any attention to this field,
and so the starting value will always be blank. Worse, the field
loses its \*(L"sticky\*(R" behavior and forgets its previous contents. The
starting value field is called for in the \s-1HTML\s0 specification, however,
and possibly some browser will eventually provide support for it.
.IP "3." 4
The optional third parameter is the size of the field in
characters (\-size).
.IP "4." 4
The optional fourth parameter is the maximum number of characters the
field will accept (\-maxlength).
.PP
\&\s-1JAVASCRIPTING:\s0 The \fB\-onChange\fR, \fB\-onFocus\fR, \fB\-onBlur\fR,
\&\fB\-onMouseOver\fR, \fB\-onMouseOut\fR and \fB\-onSelect\fR parameters are
recognized. See \fItextfield()\fR for details.
.SS "Creating a popup menu"
.IX Subsection "Creating a popup menu"
.Vb 3
\& print popup_menu(\*(Aqmenu_name\*(Aq,
\& [\*(Aqeenie\*(Aq,\*(Aqmeenie\*(Aq,\*(Aqminie\*(Aq],
\& \*(Aqmeenie\*(Aq);
\&
\& \-or\-
\&
\& %labels = (\*(Aqeenie\*(Aq=>\*(Aqyour first choice\*(Aq,
\& \*(Aqmeenie\*(Aq=>\*(Aqyour second choice\*(Aq,
\& \*(Aqminie\*(Aq=>\*(Aqyour third choice\*(Aq);
\& %attributes = (\*(Aqeenie\*(Aq=>{\*(Aqclass\*(Aq=>\*(Aqclass of first choice\*(Aq});
\& print popup_menu(\*(Aqmenu_name\*(Aq,
\& [\*(Aqeenie\*(Aq,\*(Aqmeenie\*(Aq,\*(Aqminie\*(Aq],
\& \*(Aqmeenie\*(Aq,\e%labels,\e%attributes);
\&
\& \-or (named parameter style)\-
\&
\& print popup_menu(\-name=>\*(Aqmenu_name\*(Aq,
\& \-values=>[\*(Aqeenie\*(Aq,\*(Aqmeenie\*(Aq,\*(Aqminie\*(Aq],
\& \-default=>[\*(Aqmeenie\*(Aq,\*(Aqminie\*(Aq],
\& \-labels=>\e%labels,
\& \-attributes=>\e%attributes);
.Ve
.PP
\&\fIpopup_menu()\fR creates a menu. Please note that the \-multiple option will be
ignored if passed \- use \fIscrolling_list()\fR if you want to create a menu that
supports multiple selections
.IP "1." 4
The required first argument is the menu's name (\-name).
.IP "2." 4
The required second argument (\-values) is an array \fBreference\fR
containing the list of menu items in the menu. You can pass the
method an anonymous array, as shown in the example, or a reference to
a named array, such as \*(L"\e@foo\*(R".
.IP "3." 4
The optional third parameter (\-default) is the name of the default
menu choice. If not specified, the first item will be the default.
The values of the previous choice will be maintained across
queries. Pass an array reference to select multiple defaults.
.IP "4." 4
The optional fourth parameter (\-labels) is provided for people who
want to use different values for the user-visible label inside the
popup menu and the value returned to your script. It's a pointer to an
hash relating menu values to user-visible labels. If you
leave this parameter blank, the menu values will be displayed by
default. (You can also leave a label undefined if you want to).
.IP "5." 4
The optional fifth parameter (\-attributes) is provided to assign
any of the common \s-1HTML\s0 attributes to an individual menu item. It's
a pointer to a hash relating menu values to another
hash with the attribute's name as the key and the
attribute's value as the value.
.PP
When the form is processed, the selected value of the popup menu can
be retrieved using:
.PP
.Vb 1
\& $popup_menu_value = param(\*(Aqmenu_name\*(Aq);
.Ve
.SS "Creating an option group"
.IX Subsection "Creating an option group"
Named parameter style
.PP
.Vb 9
\& print popup_menu(\-name=>\*(Aqmenu_name\*(Aq,
\& \-values=>[qw/eenie meenie minie/,
\& optgroup(\-name=>\*(Aqoptgroup_name\*(Aq,
\& \-values => [\*(Aqmoe\*(Aq,\*(Aqcatch\*(Aq],
\& \-attributes=>{\*(Aqcatch\*(Aq=>{\*(Aqclass\*(Aq=>\*(Aqred\*(Aq}})],
\& \-labels=>{\*(Aqeenie\*(Aq=>\*(Aqone\*(Aq,
\& \*(Aqmeenie\*(Aq=>\*(Aqtwo\*(Aq,
\& \*(Aqminie\*(Aq=>\*(Aqthree\*(Aq},
\& \-default=>\*(Aqmeenie\*(Aq);
\&
\& Old style
\& print popup_menu(\*(Aqmenu_name\*(Aq,
\& [\*(Aqeenie\*(Aq,\*(Aqmeenie\*(Aq,\*(Aqminie\*(Aq,
\& optgroup(\*(Aqoptgroup_name\*(Aq, [\*(Aqmoe\*(Aq, \*(Aqcatch\*(Aq],
\& {\*(Aqcatch\*(Aq=>{\*(Aqclass\*(Aq=>\*(Aqred\*(Aq}})],\*(Aqmeenie\*(Aq,
\& {\*(Aqeenie\*(Aq=>\*(Aqone\*(Aq,\*(Aqmeenie\*(Aq=>\*(Aqtwo\*(Aq,\*(Aqminie\*(Aq=>\*(Aqthree\*(Aq});
.Ve
.PP
\&\fIoptgroup()\fR creates an option group within a popup menu.
.IP "1." 4
The required first argument (\fB\-name\fR) is the label attribute of the
optgroup and is \fBnot\fR inserted in the parameter list of the query.
.IP "2." 4
The required second argument (\fB\-values\fR) is an array reference
containing the list of menu items in the menu. You can pass the
method an anonymous array, as shown in the example, or a reference
to a named array, such as \e@foo. If you pass a \s-1HASH\s0 reference,
the keys will be used for the menu values, and the values will be
used for the menu labels (see \-labels below).
.IP "3." 4
The optional third parameter (\fB\-labels\fR) allows you to pass a reference
to a hash containing user-visible labels for one or more
of the menu items. You can use this when you want the user to see one
menu string, but have the browser return your program a different one.
If you don't specify this, the value string will be used instead
(\*(L"eenie\*(R", \*(L"meenie\*(R" and \*(L"minie\*(R" in this example). This is equivalent
to using a hash reference for the \-values parameter.
.IP "4." 4
An optional fourth parameter (\fB\-labeled\fR) can be set to a true value
and indicates that the values should be used as the label attribute
for each option element within the optgroup.
.IP "5." 4
An optional fifth parameter (\-novals) can be set to a true value and
indicates to suppress the val attribute in each option element within
the optgroup.
.Sp
See the discussion on optgroup at W3C
(http://www.w3.org/TR/REC\-html40/interact/forms.html#edef\-OPTGROUP)
for details.
.IP "6." 4
An optional sixth parameter (\-attributes) is provided to assign
any of the common \s-1HTML\s0 attributes to an individual menu item. It's
a pointer to a hash relating menu values to another
hash with the attribute's name as the key and the
attribute's value as the value.
.SS "Creating a scrolling list"
.IX Subsection "Creating a scrolling list"
.Vb 4
\& print scrolling_list(\*(Aqlist_name\*(Aq,
\& [\*(Aqeenie\*(Aq,\*(Aqmeenie\*(Aq,\*(Aqminie\*(Aq,\*(Aqmoe\*(Aq],
\& [\*(Aqeenie\*(Aq,\*(Aqmoe\*(Aq],5,\*(Aqtrue\*(Aq,{\*(Aqmoe\*(Aq=>{\*(Aqclass\*(Aq=>\*(Aqred\*(Aq}});
\& \-or\-
\&
\& print scrolling_list(\*(Aqlist_name\*(Aq,
\& [\*(Aqeenie\*(Aq,\*(Aqmeenie\*(Aq,\*(Aqminie\*(Aq,\*(Aqmoe\*(Aq],
\& [\*(Aqeenie\*(Aq,\*(Aqmoe\*(Aq],5,\*(Aqtrue\*(Aq,
\& \e%labels,%attributes);
\&
\& \-or\-
\&
\& print scrolling_list(\-name=>\*(Aqlist_name\*(Aq,
\& \-values=>[\*(Aqeenie\*(Aq,\*(Aqmeenie\*(Aq,\*(Aqminie\*(Aq,\*(Aqmoe\*(Aq],
\& \-default=>[\*(Aqeenie\*(Aq,\*(Aqmoe\*(Aq],
\& \-size=>5,
\& \-multiple=>\*(Aqtrue\*(Aq,
\& \-labels=>\e%labels,
\& \-attributes=>\e%attributes);
.Ve
.PP
\&\fIscrolling_list()\fR creates a scrolling list.
.PP
\&\fBParameters:\fR
.IP "1." 4
The first and second arguments are the list name (\-name) and values
(\-values). As in the popup menu, the second argument should be an
array reference.
.IP "2." 4
The optional third argument (\-default) can be either a reference to a
list containing the values to be selected by default, or can be a
single value to select. If this argument is missing or undefined,
then nothing is selected when the list first appears. In the named
parameter version, you can use the synonym \*(L"\-defaults\*(R" for this
parameter.
.IP "3." 4
The optional fourth argument is the size of the list (\-size).
.IP "4." 4
The optional fifth argument can be set to true to allow multiple
simultaneous selections (\-multiple). Otherwise only one selection
will be allowed at a time.
.IP "5." 4
The optional sixth argument is a pointer to a hash
containing long user-visible labels for the list items (\-labels).
If not provided, the values will be displayed.
.IP "6." 4
The optional sixth parameter (\-attributes) is provided to assign
any of the common \s-1HTML\s0 attributes to an individual menu item. It's
a pointer to a hash relating menu values to another
hash with the attribute's name as the key and the
attribute's value as the value.
.Sp
When this form is processed, all selected list items will be returned as
a list under the parameter name 'list_name'. The values of the
selected items can be retrieved with:
.Sp
.Vb 1
\& @selected = param(\*(Aqlist_name\*(Aq);
.Ve
.SS "Creating a group of related checkboxes"
.IX Subsection "Creating a group of related checkboxes"
.Vb 7
\& print checkbox_group(\-name=>\*(Aqgroup_name\*(Aq,
\& \-values=>[\*(Aqeenie\*(Aq,\*(Aqmeenie\*(Aq,\*(Aqminie\*(Aq,\*(Aqmoe\*(Aq],
\& \-default=>[\*(Aqeenie\*(Aq,\*(Aqmoe\*(Aq],
\& \-linebreak=>\*(Aqtrue\*(Aq,
\& \-disabled => [\*(Aqmoe\*(Aq],
\& \-labels=>\e%labels,
\& \-attributes=>\e%attributes);
\&
\& print checkbox_group(\*(Aqgroup_name\*(Aq,
\& [\*(Aqeenie\*(Aq,\*(Aqmeenie\*(Aq,\*(Aqminie\*(Aq,\*(Aqmoe\*(Aq],
\& [\*(Aqeenie\*(Aq,\*(Aqmoe\*(Aq],\*(Aqtrue\*(Aq,\e%labels,
\& {\*(Aqmoe\*(Aq=>{\*(Aqclass\*(Aq=>\*(Aqred\*(Aq}});
\&
\& HTML3\-COMPATIBLE BROWSERS ONLY:
\&
\& print checkbox_group(\-name=>\*(Aqgroup_name\*(Aq,
\& \-values=>[\*(Aqeenie\*(Aq,\*(Aqmeenie\*(Aq,\*(Aqminie\*(Aq,\*(Aqmoe\*(Aq],
\& \-rows=2,\-columns=>2);
.Ve
.PP
\&\fIcheckbox_group()\fR creates a list of checkboxes that are related
by the same name.
.PP
\&\fBParameters:\fR
.IP "1." 4
The first and second arguments are the checkbox name and values,
respectively (\-name and \-values). As in the popup menu, the second
argument should be an array reference. These values are used for the
user-readable labels printed next to the checkboxes as well as for the
values passed to your script in the query string.
.IP "2." 4
The optional third argument (\-default) can be either a reference to a
list containing the values to be checked by default, or can be a
single value to checked. If this argument is missing or undefined,
then nothing is selected when the list first appears.
.IP "3." 4
The optional fourth argument (\-linebreak) can be set to true to place
line breaks between the checkboxes so that they appear as a vertical
list. Otherwise, they will be strung together on a horizontal line.
.PP
The optional \fB\-labels\fR argument is a pointer to a hash
relating the checkbox values to the user-visible labels that will be
printed next to them. If not provided, the values will be used as the
default.
.PP
The optional parameters \fB\-rows\fR, and \fB\-columns\fR cause
\&\fIcheckbox_group()\fR to return an \s-1HTML3\s0 compatible table containing the
checkbox group formatted with the specified number of rows and
columns. You can provide just the \-columns parameter if you wish;
checkbox_group will calculate the correct number of rows for you.
.PP
The option \fB\-disabled\fR takes an array of checkbox values and disables
them by greying them out (this may not be supported by all browsers).
.PP
The optional \fB\-attributes\fR argument is provided to assign any of the
common \s-1HTML\s0 attributes to an individual menu item. It's a pointer to
a hash relating menu values to another hash
with the attribute's name as the key and the attribute's value as the
value.
.PP
The optional \fB\-tabindex\fR argument can be used to control the order in which
radio buttons receive focus when the user presses the tab button. If
passed a scalar numeric value, the first element in the group will
receive this tab index and subsequent elements will be incremented by
one. If given a reference to an array of radio button values, then
the indexes will be jiggered so that the order specified in the array
will correspond to the tab order. You can also pass a reference to a
hash in which the hash keys are the radio button values and the values
are the tab indexes of each button. Examples:
.PP
.Vb 3
\& \-tabindex => 100 # this group starts at index 100 and counts up
\& \-tabindex => [\*(Aqmoe\*(Aq,\*(Aqminie\*(Aq,\*(Aqeenie\*(Aq,\*(Aqmeenie\*(Aq] # tab in this order
\& \-tabindex => {meenie=>100,moe=>101,minie=>102,eenie=>200} # tab in this order
.Ve
.PP
The optional \fB\-labelattributes\fR argument will contain attributes
attached to the <label> element that surrounds each button.
.PP
When the form is processed, all checked boxes will be returned as
a list under the parameter name 'group_name'. The values of the
\&\*(L"on\*(R" checkboxes can be retrieved with:
.PP
.Vb 1
\& @turned_on = param(\*(Aqgroup_name\*(Aq);
.Ve
.PP
The value returned by \fIcheckbox_group()\fR is actually an array of button
elements. You can capture them and use them within tables, lists,
or in other creative ways:
.PP
.Vb 2
\& @h = checkbox_group(\-name=>\*(Aqgroup_name\*(Aq,\-values=>\e@values);
\& &use_in_creative_way(@h);
.Ve
.SS "Creating a standalone checkbox"
.IX Subsection "Creating a standalone checkbox"
.Vb 4
\& print checkbox(\-name=>\*(Aqcheckbox_name\*(Aq,
\& \-checked=>1,
\& \-value=>\*(AqON\*(Aq,
\& \-label=>\*(AqCLICK ME\*(Aq);
\&
\& \-or\-
\&
\& print checkbox(\*(Aqcheckbox_name\*(Aq,\*(Aqchecked\*(Aq,\*(AqON\*(Aq,\*(AqCLICK ME\*(Aq);
.Ve
.PP
\&\fIcheckbox()\fR is used to create an isolated checkbox that isn't logically
related to any others.
.PP
\&\fBParameters:\fR
.IP "1." 4
The first parameter is the required name for the checkbox (\-name). It
will also be used for the user-readable label printed next to the
checkbox.
.IP "2." 4
The optional second parameter (\-checked) specifies that the checkbox
is turned on by default. Synonyms are \-selected and \-on.
.IP "3." 4
The optional third parameter (\-value) specifies the value of the
checkbox when it is checked. If not provided, the word \*(L"on\*(R" is
assumed.
.IP "4." 4
The optional fourth parameter (\-label) is the user-readable label to
be attached to the checkbox. If not provided, the checkbox name is
used.
.PP
The value of the checkbox can be retrieved using:
.PP
.Vb 1
\& $turned_on = param(\*(Aqcheckbox_name\*(Aq);
.Ve
.SS "Creating a radio button group"
.IX Subsection "Creating a radio button group"
.Vb 6
\& print radio_group(\-name=>\*(Aqgroup_name\*(Aq,
\& \-values=>[\*(Aqeenie\*(Aq,\*(Aqmeenie\*(Aq,\*(Aqminie\*(Aq],
\& \-default=>\*(Aqmeenie\*(Aq,
\& \-linebreak=>\*(Aqtrue\*(Aq,
\& \-labels=>\e%labels,
\& \-attributes=>\e%attributes);
\&
\& \-or\-
\&
\& print radio_group(\*(Aqgroup_name\*(Aq,[\*(Aqeenie\*(Aq,\*(Aqmeenie\*(Aq,\*(Aqminie\*(Aq],
\& \*(Aqmeenie\*(Aq,\*(Aqtrue\*(Aq,\e%labels,\e%attributes);
\&
\&
\& HTML3\-COMPATIBLE BROWSERS ONLY:
\&
\& print radio_group(\-name=>\*(Aqgroup_name\*(Aq,
\& \-values=>[\*(Aqeenie\*(Aq,\*(Aqmeenie\*(Aq,\*(Aqminie\*(Aq,\*(Aqmoe\*(Aq],
\& \-rows=2,\-columns=>2);
.Ve
.PP
\&\fIradio_group()\fR creates a set of logically-related radio buttons
(turning one member of the group on turns the others off)
.PP
\&\fBParameters:\fR
.IP "1." 4
The first argument is the name of the group and is required (\-name).
.IP "2." 4
The second argument (\-values) is the list of values for the radio
buttons. The values and the labels that appear on the page are
identical. Pass an array \fIreference\fR in the second argument, either
using an anonymous array, as shown, or by referencing a named array as
in \*(L"\e@foo\*(R".
.IP "3." 4
The optional third parameter (\-default) is the name of the default
button to turn on. If not specified, the first item will be the
default. You can provide a nonexistent button name, such as \*(L"\-\*(R" to
start up with no buttons selected.
.IP "4." 4
The optional fourth parameter (\-linebreak) can be set to 'true' to put
line breaks between the buttons, creating a vertical list.
.IP "5." 4
The optional fifth parameter (\-labels) is a pointer to an associative
array relating the radio button values to user-visible labels to be
used in the display. If not provided, the values themselves are
displayed.
.PP
All modern browsers can take advantage of the optional parameters
\&\fB\-rows\fR, and \fB\-columns\fR. These parameters cause \fIradio_group()\fR to
return an \s-1HTML3\s0 compatible table containing the radio group formatted
with the specified number of rows and columns. You can provide just
the \-columns parameter if you wish; radio_group will calculate the
correct number of rows for you.
.PP
To include row and column headings in the returned table, you
can use the \fB\-rowheaders\fR and \fB\-colheaders\fR parameters. Both
of these accept a pointer to an array of headings to use.
The headings are just decorative. They don't reorganize the
interpretation of the radio buttons \*(-- they're still a single named
unit.
.PP
The optional \fB\-tabindex\fR argument can be used to control the order in which
radio buttons receive focus when the user presses the tab button. If
passed a scalar numeric value, the first element in the group will
receive this tab index and subsequent elements will be incremented by
one. If given a reference to an array of radio button values, then
the indexes will be jiggered so that the order specified in the array
will correspond to the tab order. You can also pass a reference to a
hash in which the hash keys are the radio button values and the values
are the tab indexes of each button. Examples:
.PP
.Vb 3
\& \-tabindex => 100 # this group starts at index 100 and counts up
\& \-tabindex => [\*(Aqmoe\*(Aq,\*(Aqminie\*(Aq,\*(Aqeenie\*(Aq,\*(Aqmeenie\*(Aq] # tab in this order
\& \-tabindex => {meenie=>100,moe=>101,minie=>102,eenie=>200} # tab in this order
.Ve
.PP
The optional \fB\-attributes\fR argument is provided to assign any of the
common \s-1HTML\s0 attributes to an individual menu item. It's a pointer to
a hash relating menu values to another hash
with the attribute's name as the key and the attribute's value as the
value.
.PP
The optional \fB\-labelattributes\fR argument will contain attributes
attached to the <label> element that surrounds each button.
.PP
When the form is processed, the selected radio button can
be retrieved using:
.PP
.Vb 1
\& $which_radio_button = param(\*(Aqgroup_name\*(Aq);
.Ve
.PP
The value returned by \fIradio_group()\fR is actually an array of button
elements. You can capture them and use them within tables, lists,
or in other creative ways:
.PP
.Vb 2
\& @h = radio_group(\-name=>\*(Aqgroup_name\*(Aq,\-values=>\e@values);
\& &use_in_creative_way(@h);
.Ve
.SS "Creating a submit button"
.IX Subsection "Creating a submit button"
.Vb 2
\& print submit(\-name=>\*(Aqbutton_name\*(Aq,
\& \-value=>\*(Aqvalue\*(Aq);
\&
\& \-or\-
\&
\& print submit(\*(Aqbutton_name\*(Aq,\*(Aqvalue\*(Aq);
.Ve
.PP
\&\fIsubmit()\fR will create the query submission button. Every form
should have one of these.
.PP
\&\fBParameters:\fR
.IP "1." 4
The first argument (\-name) is optional. You can give the button a
name if you have several submission buttons in your form and you want
to distinguish between them.
.IP "2." 4
The second argument (\-value) is also optional. This gives the button
a value that will be passed to your script in the query string. The
name will also be used as the user-visible label.
.IP "3." 4
You can use \-label as an alias for \-value. I always get confused
about which of \-name and \-value changes the user-visible label on the
button.
.PP
You can figure out which button was pressed by using different
values for each one:
.PP
.Vb 1
\& $which_one = param(\*(Aqbutton_name\*(Aq);
.Ve
.SS "Creating a reset button"
.IX Subsection "Creating a reset button"
.Vb 1
\& print reset
.Ve
.PP
\&\fIreset()\fR creates the \*(L"reset\*(R" button. Note that it restores the
form to its value from the last time the script was called,
\&\s-1NOT\s0 necessarily to the defaults.
.PP
Note that this conflicts with the perl \fIreset()\fR built-in. Use
\&\fICORE::reset()\fR to get the original reset function.
.SS "Creating a default button"
.IX Subsection "Creating a default button"
.Vb 1
\& print defaults(\*(Aqbutton_label\*(Aq)
.Ve
.PP
\&\fIdefaults()\fR creates a button that, when invoked, will cause the
form to be completely reset to its defaults, wiping out all the
changes the user ever made.
.SS "Creating a hidden field"
.IX Subsection "Creating a hidden field"
.Vb 2
\& print hidden(\-name=>\*(Aqhidden_name\*(Aq,
\& \-default=>[\*(Aqvalue1\*(Aq,\*(Aqvalue2\*(Aq...]);
\&
\& \-or\-
\&
\& print hidden(\*(Aqhidden_name\*(Aq,\*(Aqvalue1\*(Aq,\*(Aqvalue2\*(Aq...);
.Ve
.PP
\&\fIhidden()\fR produces a text field that can't be seen by the user. It
is useful for passing state variable information from one invocation
of the script to the next.
.PP
\&\fBParameters:\fR
.IP "1." 4
The first argument is required and specifies the name of this
field (\-name).
.IP "2." 4
The second argument is also required and specifies its value
(\-default). In the named parameter style of calling, you can provide
a single value here or a reference to a whole list
.PP
Fetch the value of a hidden field this way:
.PP
.Vb 1
\& $hidden_value = param(\*(Aqhidden_name\*(Aq);
.Ve
.PP
Note, that just like all the other form elements, the value of a
hidden field is \*(L"sticky\*(R". If you want to replace a hidden field with
some other values after the script has been called once you'll have to
do it manually:
.PP
.Vb 1
\& param(\*(Aqhidden_name\*(Aq,\*(Aqnew\*(Aq,\*(Aqvalues\*(Aq,\*(Aqhere\*(Aq);
.Ve
.SS "Creating a clickable image button"
.IX Subsection "Creating a clickable image button"
.Vb 3
\& print image_button(\-name=>\*(Aqbutton_name\*(Aq,
\& \-src=>\*(Aq/source/URL\*(Aq,
\& \-align=>\*(AqMIDDLE\*(Aq);
\&
\& \-or\-
\&
\& print image_button(\*(Aqbutton_name\*(Aq,\*(Aq/source/URL\*(Aq,\*(AqMIDDLE\*(Aq);
.Ve
.PP
\&\fIimage_button()\fR produces a clickable image. When it's clicked on the
position of the click is returned to your script as \*(L"button_name.x\*(R"
and \*(L"button_name.y\*(R", where \*(L"button_name\*(R" is the name you've assigned
to it.
.PP
\&\fBParameters:\fR
.IP "1." 4
The first argument (\-name) is required and specifies the name of this
field.
.IP "2." 4
The second argument (\-src) is also required and specifies the \s-1URL\s0
.IP "3." 4
The third option (\-align, optional) is an alignment type, and may be
\&\s-1TOP, BOTTOM\s0 or \s-1MIDDLE\s0
.PP
Fetch the value of the button this way:
\f(CW$x\fR = param('button_name.x');
\f(CW$y\fR = param('button_name.y');
.SS "Creating a javascript action button"
.IX Subsection "Creating a javascript action button"
.Vb 3
\& print button(\-name=>\*(Aqbutton_name\*(Aq,
\& \-value=>\*(Aquser visible label\*(Aq,
\& \-onClick=>"do_something()");
\&
\& \-or\-
\&
\& print button(\*(Aqbutton_name\*(Aq,"user visible value","do_something()");
.Ve
.PP
\&\fIbutton()\fR produces an \f(CW\*(C`<input>\*(C'\fR tag with \f(CW\*(C`type="button"\*(C'\fR. When it's
pressed the fragment of JavaScript code pointed to by the \fB\-onClick\fR parameter
will be executed.
.SH "WORKING WITH FRAMES"
.IX Header "WORKING WITH FRAMES"
It's possible for \s-1CGI\s0.pm scripts to write into several browser panels
and windows using the \s-1HTML 4\s0 frame mechanism. There are three
techniques for defining new frames programmatically:
.IP "1. Create a <Frameset> document" 4
.IX Item "1. Create a <Frameset> document"
After writing out the \s-1HTTP\s0 header, instead of creating a standard
\&\s-1HTML\s0 document using the \fIstart_html()\fR call, create a <frameset>
document that defines the frames on the page. Specify your script(s)
(with appropriate parameters) as the \s-1SRC\s0 for each of the frames.
.Sp
There is no specific support for creating <frameset> sections
in \s-1CGI\s0.pm, but the \s-1HTML\s0 is very simple to write.
.IP "2. Specify the destination for the document in the \s-1HTTP\s0 header" 4
.IX Item "2. Specify the destination for the document in the HTTP header"
You may provide a \fB\-target\fR parameter to the \fIheader()\fR method:
.Sp
.Vb 1
\& print header(\-target=>\*(AqResultsWindow\*(Aq);
.Ve
.Sp
This will tell the browser to load the output of your script into the
frame named \*(L"ResultsWindow\*(R". If a frame of that name doesn't already
exist, the browser will pop up a new window and load your script's
document into that. There are a number of magic names that you can
use for targets. See the \s-1HTML \s0\f(CW\*(C`<frame>\*(C'\fR documentation for details.
.IP "3. Specify the destination for the document in the <form> tag" 4
.IX Item "3. Specify the destination for the document in the <form> tag"
You can specify the frame to load in the \s-1FORM\s0 tag itself. With
\&\s-1CGI\s0.pm it looks like this:
.Sp
.Vb 1
\& print start_form(\-target=>\*(AqResultsWindow\*(Aq);
.Ve
.Sp
When your script is reinvoked by the form, its output will be loaded
into the frame named \*(L"ResultsWindow\*(R". If one doesn't already exist
a new window will be created.
.PP
The script \*(L"frameset.cgi\*(R" in the examples directory shows one way to
create pages in which the fill-out form and the response live in
side-by-side frames.
.SH "SUPPORT FOR JAVASCRIPT"
.IX Header "SUPPORT FOR JAVASCRIPT"
The usual way to use JavaScript is to define a set of functions in a
<\s-1SCRIPT\s0> block inside the \s-1HTML\s0 header and then to register event
handlers in the various elements of the page. Events include such
things as the mouse passing over a form element, a button being
clicked, the contents of a text field changing, or a form being
submitted. When an event occurs that involves an element that has
registered an event handler, its associated JavaScript code gets
called.
.PP
The elements that can register event handlers include the <\s-1BODY\s0> of an
\&\s-1HTML\s0 document, hypertext links, all the various elements of a fill-out
form, and the form itself. There are a large number of events, and
each applies only to the elements for which it is relevant. Here is a
partial list:
.IP "\fBonLoad\fR" 4
.IX Item "onLoad"
The browser is loading the current document. Valid in:
.Sp
.Vb 1
\& + The HTML <BODY> section only.
.Ve
.IP "\fBonUnload\fR" 4
.IX Item "onUnload"
The browser is closing the current page or frame. Valid for:
.Sp
.Vb 1
\& + The HTML <BODY> section only.
.Ve
.IP "\fBonSubmit\fR" 4
.IX Item "onSubmit"
The user has pressed the submit button of a form. This event happens
just before the form is submitted, and your function can return a
value of false in order to abort the submission. Valid for:
.Sp
.Vb 1
\& + Forms only.
.Ve
.IP "\fBonClick\fR" 4
.IX Item "onClick"
The mouse has clicked on an item in a fill-out form. Valid for:
.Sp
.Vb 3
\& + Buttons (including submit, reset, and image buttons)
\& + Checkboxes
\& + Radio buttons
.Ve
.IP "\fBonChange\fR" 4
.IX Item "onChange"
The user has changed the contents of a field. Valid for:
.Sp
.Vb 6
\& + Text fields
\& + Text areas
\& + Password fields
\& + File fields
\& + Popup Menus
\& + Scrolling lists
.Ve
.IP "\fBonFocus\fR" 4
.IX Item "onFocus"
The user has selected a field to work with. Valid for:
.Sp
.Vb 6
\& + Text fields
\& + Text areas
\& + Password fields
\& + File fields
\& + Popup Menus
\& + Scrolling lists
.Ve
.IP "\fBonBlur\fR" 4
.IX Item "onBlur"
The user has deselected a field (gone to work somewhere else). Valid
for:
.Sp
.Vb 6
\& + Text fields
\& + Text areas
\& + Password fields
\& + File fields
\& + Popup Menus
\& + Scrolling lists
.Ve
.IP "\fBonSelect\fR" 4
.IX Item "onSelect"
The user has changed the part of a text field that is selected. Valid
for:
.Sp
.Vb 4
\& + Text fields
\& + Text areas
\& + Password fields
\& + File fields
.Ve
.IP "\fBonMouseOver\fR" 4
.IX Item "onMouseOver"
The mouse has moved over an element.
.Sp
.Vb 6
\& + Text fields
\& + Text areas
\& + Password fields
\& + File fields
\& + Popup Menus
\& + Scrolling lists
.Ve
.IP "\fBonMouseOut\fR" 4
.IX Item "onMouseOut"
The mouse has moved off an element.
.Sp
.Vb 6
\& + Text fields
\& + Text areas
\& + Password fields
\& + File fields
\& + Popup Menus
\& + Scrolling lists
.Ve
.PP
In order to register a JavaScript event handler with an \s-1HTML\s0 element,
just use the event name as a parameter when you call the corresponding
\&\s-1CGI\s0 method. For example, to have your \fIvalidateAge()\fR JavaScript code
executed every time the textfield named \*(L"age\*(R" changes, generate the
field like this:
.PP
.Vb 1
\& print textfield(\-name=>\*(Aqage\*(Aq,\-onChange=>"validateAge(this)");
.Ve
.PP
This example assumes that you've already declared the \fIvalidateAge()\fR
function by incorporating it into a <\s-1SCRIPT\s0> block. The \s-1CGI\s0.pm
\&\fIstart_html()\fR method provides a convenient way to create this section.
.PP
Similarly, you can create a form that checks itself over for
consistency and alerts the user if some essential value is missing by
creating it this way:
print start_form(\-onSubmit=>\*(L"validateMe(this)\*(R");
.PP
See the javascript.cgi script for a demonstration of how this all
works.
.SH "LIMITED SUPPORT FOR CASCADING STYLE SHEETS"
.IX Header "LIMITED SUPPORT FOR CASCADING STYLE SHEETS"
\&\s-1CGI\s0.pm has limited support for \s-1HTML3\s0's cascading style sheets (css).
To incorporate a stylesheet into your document, pass the
\&\fIstart_html()\fR method a \fB\-style\fR parameter. The value of this
parameter may be a scalar, in which case it is treated as the source
\&\s-1URL\s0 for the stylesheet, or it may be a hash reference. In the latter
case you should provide the hash with one or more of \fB\-src\fR or
\&\fB\-code\fR. \fB\-src\fR points to a \s-1URL\s0 where an externally-defined
stylesheet can be found. \fB\-code\fR points to a scalar value to be
incorporated into a <style> section. Style definitions in \fB\-code\fR
override similarly-named ones in \fB\-src\fR, hence the name \*(L"cascading.\*(R"
.PP
You may also specify the type of the stylesheet by adding the optional
\&\fB\-type\fR parameter to the hash pointed to by \fB\-style\fR. If not
specified, the style defaults to 'text/css'.
.PP
To refer to a style within the body of your document, add the
\&\fB\-class\fR parameter to any \s-1HTML\s0 element:
.PP
.Vb 1
\& print h1({\-class=>\*(AqFancy\*(Aq},\*(AqWelcome to the Party\*(Aq);
.Ve
.PP
Or define styles on the fly with the \fB\-style\fR parameter:
.PP
.Vb 1
\& print h1({\-style=>\*(AqColor: red;\*(Aq},\*(AqWelcome to Hell\*(Aq);
.Ve
.PP
You may also use the new \fB\f(BIspan()\fB\fR element to apply a style to a
section of text:
.PP
.Vb 4
\& print span({\-style=>\*(AqColor: red;\*(Aq},
\& h1(\*(AqWelcome to Hell\*(Aq),
\& "Where did that handbasket get to?"
\& );
.Ve
.PP
Note that you must import the \*(L":html3\*(R" definitions to have the
\&\fB\f(BIspan()\fB\fR method available. Here's a quick and dirty example of using
\&\s-1CSS\s0's. See the \s-1CSS\s0 specification at
http://www.w3.org/Style/CSS/ for more information.
.PP
.Vb 1
\& use CGI qw/:standard :html3/;
\&
\& #here\*(Aqs a stylesheet incorporated directly into the page
\& $newStyle=<<END;
\& <!\-\-
\& P.Tip {
\& margin\-right: 50pt;
\& margin\-left: 50pt;
\& color: red;
\& }
\& P.Alert {
\& font\-size: 30pt;
\& font\-family: sans\-serif;
\& color: red;
\& }
\& \-\->
\& END
\& print header();
\& print start_html( \-title=>\*(AqCGI with Style\*(Aq,
\& \-style=>{\-src=>\*(Aqhttp://www.capricorn.com/style/st1.css\*(Aq,
\& \-code=>$newStyle}
\& );
\& print h1(\*(AqCGI with Style\*(Aq),
\& p({\-class=>\*(AqTip\*(Aq},
\& "Better read the cascading style sheet spec before playing with this!"),
\& span({\-style=>\*(Aqcolor: magenta\*(Aq},
\& "Look Mom, no hands!",
\& p(),
\& "Whooo wee!"
\& );
\& print end_html;
.Ve
.PP
Pass an array reference to \fB\-code\fR or \fB\-src\fR in order to incorporate
multiple stylesheets into your document.
.PP
Should you wish to incorporate a verbatim stylesheet that includes
arbitrary formatting in the header, you may pass a \-verbatim tag to
the \-style hash, as follows:
.PP
print start_html (\-style => {\-verbatim => '@import url(\*(L"/server\-common/css/'.$cssFile.'\*(R");',
\-src => '/server\-common/css/core.css'});
.PP
This will generate an \s-1HTML\s0 header that contains this:
.PP
.Vb 4
\& <link rel="stylesheet" type="text/css" href="/server\-common/css/core.css">
\& <style type="text/css">
\& @import url("/server\-common/css/main.css");
\& </style>
.Ve
.PP
Any additional arguments passed in the \-style value will be
incorporated into the <link> tag. For example:
.PP
.Vb 2
\& start_html(\-style=>{\-src=>[\*(Aq/styles/print.css\*(Aq,\*(Aq/styles/layout.css\*(Aq],
\& \-media => \*(Aqall\*(Aq});
.Ve
.PP
This will give:
.PP
.Vb 2
\& <link rel="stylesheet" type="text/css" href="/styles/print.css" media="all"/>
\& <link rel="stylesheet" type="text/css" href="/styles/layout.css" media="all"/>
.Ve
.PP
<p>
.PP
To make more complicated <link> tags, use the \fILink()\fR function
and pass it to \fIstart_html()\fR in the \-head argument, as in:
.PP
.Vb 3
\& @h = (Link({\-rel=>\*(Aqstylesheet\*(Aq,\-type=>\*(Aqtext/css\*(Aq,\-src=>\*(Aq/ss/ss.css\*(Aq,\-media=>\*(Aqall\*(Aq}),
\& Link({\-rel=>\*(Aqstylesheet\*(Aq,\-type=>\*(Aqtext/css\*(Aq,\-src=>\*(Aq/ss/fred.css\*(Aq,\-media=>\*(Aqpaper\*(Aq}));
\& print start_html({\-head=>\e@h})
.Ve
.PP
To create primary and \*(L"alternate\*(R" stylesheet, use the \fB\-alternate\fR option:
.PP
.Vb 5
\& start_html(\-style=>{\-src=>[
\& {\-src=>\*(Aq/styles/print.css\*(Aq},
\& {\-src=>\*(Aq/styles/alt.css\*(Aq,\-alternate=>1}
\& ]
\& });
.Ve
.SS "Dumping out all the name/value pairs"
.IX Subsection "Dumping out all the name/value pairs"
The \fIDump()\fR method produces a string consisting of all the query's name/value
pairs formatted nicely as a nested list. This is useful for debugging purposes:
.PP
.Vb 1
\& print Dump
.Ve
.PP
Produces something that looks like:
.PP
.Vb 11
\& <ul>
\& <li>name1
\& <ul>
\& <li>value1
\& <li>value2
\& </ul>
\& <li>name2
\& <ul>
\& <li>value1
\& </ul>
\& </ul>
.Ve
.PP
As a shortcut, you can interpolate the entire \s-1CGI\s0 object into a string
and it will be replaced with the a nice \s-1HTML\s0 dump shown above:
.PP
.Vb 2
\& $q=CGI\->new;
\& print "<h2>Current Values</h2> $q\en";
.Ve
.SH "BUGS"
.IX Header "BUGS"
Address bug reports and comments to: <https://github.com/leejo/CGI.pm/issues>
.PP
See the <https://github.com/leejo/CGI.pm/blob/master/CONTRIBUTING.md> file for information
on raising issues and contributing
.PP
The original bug tracker can be found at:
<https://rt.cpan.org/Public/Dist/Display.html?Queue=CGI.pm>
.SH "SEE ALSO"
.IX Header "SEE ALSO"
\&\s-1CGI\s0 \- The original source of this documentation / functionality