Database Reference
In-Depth Information
the group as you want selected by default. If no items are marked as
checked
, none
are selected initially. The following checkbox set shows the cow accessory items
with the first two items selected by default:
<input
type=
"checkbox"
name=
"accessories"
value=
"cow bell"
checked=
"checked"
/>
cow bell
<input
type=
"checkbox"
name=
"accessories"
value=
"horns"
checked=
"checked"
/>
horns
<input
type=
"checkbox"
name=
"accessories"
value=
"nose ring"
/>
nose ring
<input
type=
"checkbox"
name=
"accessories"
value=
"tail ribbon"
/>
tail ribbon
Scrolling lists
A multiple-pick scrolling list has most syntax in common with its single-pick coun‐
terpart. The differences are that you include a
multiple
attribute in the opening
<select>
tag, and default value selection differs. For a single-pick list, add
select
ed
to at most one item; in the absence of an explicit
selected
attribute, the first
item is selected by default. For a multiple-pick list, add a
selected
attribute to as
many of the items as you like; in the absence of
selected
attributes, no items are
selected by default.
Represented as a multiple-pick scrolling list with
cow
bell
and
horns
selected ini‐
tially, the set of cow accessories looks like this:
<select
name=
"accessories"
size=
"3"
multiple=
"multiple"
>
<option
value=
"cow bell"
selected=
"selected"
>
cow bell
</option>
<option
value=
"horns"
selected=
"selected"
>
horns
</option>
<option
value=
"nose ring"
>
nose ring
</option>
<option
value=
"tail ribbon"
>
tail ribbon
</option>
</select>
In CGI.pm-based Perl scripts, create checkbox sets or scrolling lists by invoking
check
box_group()
or
scrolling_list()
. These functions take
name
,
values
,
labels
, and
default
arguments, just like their single-pick cousins. But because multiple items can
be selected initially, CGI.pm permits the
default
argument to be specified as either a
scalar value or a reference to an array of values. It also accepts the argument name
defaults
as a synonym for
default
.
To get the list of legal values for a
SET
column, do the same thing as in
Recipe 20.2
for
ENUM
columns—invoke a utility routine that returns the column metadata:
my
$acc_info
=
get_enumorset_info
(
$dbh
,
"cookbook"
,
"cow_order"
,
"accessories"
);
However, the default value for a
SET
column is not in a form that is directly usable for
form element generation. MySQL represents
SET
default values as a list of zero or more
items, separated by commas; for example, the default for the
accessories
column is
cow bell,horns
. That doesn't match the list-of-values format that the CGI.pm func‐
tions expect, so it's necessary to split the default value at the commas to obtain an array.
The following expression shows how, taking into account the possibility that the default
column value might be
undef
(
NULL
):