Database Reference
In-Depth Information
name
The form element name.
values
An array or list of values for the items in the element.
labels
Another array that provides the corresponding element label to display for each
value. The two arrays must be the same size. (To use the values as the labels, pass
the same array to the function twice.)
default
The initial value of the form element. This should be a scalar value, except for
make_scrolling_list()
. We'll write that function to handle either single-pick or
multiple-pick lists (and use it for the latter purpose in
Recipe 20.3
), so its
de
fault
value is permitted to be either a scalar or an array. If there is no default, pass
a value not present in the
values
array; typically, an empty string will do.
Some of the functions have additional arguments that apply only to particular element
types:
vertical
This applies to radio button groups. If true, items are stacked vertically rather than
horizontally.
size
,
multiple
These arguments apply to scrolling lists.
size
indicates how many items in the list
are visible, and
multiple
should be true if the list permits multiple selections.
The implementation of some of these list-generating functions is discussed here, but
you can find the code for all of them in the
lib
directory of the
recipes
distribution. All
of them act like CGI.pm for form element functions in the sense that they automatically
perform HTML-encoding of argument values that are incorporated into the list. (The
Ruby version of the library file includes utility methods for generating these elements,
too, even though the
cgi
module has methods for creating them. I think the utility
methods are easier to use than the
cgi
methods.)
In PHP, the
make_radio_group()
function for creating a set of radio buttons looks like
this:
function
make_radio_group
(
$name
,
$values
,
$labels
,
$default
,
$vertical
)
{
$result
=
''
;
for
(
$i
=
0
;
$i
<
count
(
$values
);
$i
++
)
{
# select the item if it corresponds to the default value
$checked
=
(
$values
[
$i
]
==
$default
?
' checked="checked"'
:
''
);
$result
.=
sprintf
(
'<input type="radio" name="%s" value="%s"%s />%s'
,