Database Reference
In-Depth Information
cust_city: Bald Knob
cust_state: AR
To generate a form with contents that correspond to a database record, use the column
values for the element defaults as follows:
• For
<input>
elements such as radio buttons or checkboxes, add a
checked
attribute
to each list item that matches the column value.
• For
<select>
elements such as pop-up menus or scrolling lists, add a
selected
attribute to each list item that matches the column value.
• For text fields represented as
<input>
elements of type
text
, set the
value
attribute
to the corresponding column value. For example, to present a 60-character field for
cust_name
, initialized to
Farmer
Brown
, do this:
<input
type=
"text"
name=
"cust_name"
value=
"Farmer Brown"
size=
"60"
/>
To present a
<textarea>
element instead, set the body to the column value. To
create a field 40 columns wide and 3 rows high, write it like this:
<textarea
name=
"cust_name"
cols=
"40"
rows=
"3"
>
Farmer Brown
</textarea>
• In a record-editing situation, it's a good idea to include a unique value in the form
so that you can tell which record the form contents represent when the user submits
it. Use a hidden field to do this. Its value is not displayed to the user, but the browser
returns it with the rest of the field values. Our sample record has an
id
column with
a value of
1
, so the hidden field looks like this:
<input
type=
"hidden"
name=
"id"
value=
"1"
/>
The following examples show how to produce a form with
id
represented as a hidden
field,
color
as a pop-up menu,
size
as a set of radio buttons, and
accessories
as a set
of checkboxes. The customer information values are represented as text input boxes,
except that
cust_state
is a single-pick scrolling list. You could make other choices, of
course, such as to present the sizes as a pop-up menu rather than as radio buttons.
The
recipes
distribution scripts for the examples in this section are named
cow_ed‐
it.pl
,
cow_edit.jsp
, and so forth. Note that these scripts are designed only to present the
entry form; they do nothing with the form contents when you click the Submit button.
The following procedure outlines how to load the sample
cow_table
record into an
editing form for a CGI.pm-based Perl script:
1. Retrieve the column values for the record to load into the form:
my
$id
=
1
;
# select record number 1
my
(
$color
,
$size
,
$accessories
,
$cust_name
,
$cust_street
,
$cust_city
,
$cust_state
)
=
$dbh
->
selectrow_array
(
qq{