Case Study: Producing a select List
To
conclude this chapter, we present a short case study of dynamically
producing <form> components from a database.
The techniques used are an application of the five-step querying
process from Chapter 4.
We have already identified that the scripts in most of this
chapter’s examples require that the user remember
and reproduce the names of the wine regions. A far better
approach—and one that works well for small numbers of
items—is to present values using the HTML
<select> input type. For the wine regions,
the <select> input has the following
structure:
<select name="regionName"> <option selected> All <option> Barossa Valley <option> Coonawarra <option> Goulburn Valley <option> Lower Hunter Valley <option> Margaret River <option> Riverland <option> Rutherglen <option> Swan Valley <option> Upper Hunter Valley </select>
With only a small number of wine regions, it is tempting to develop a
static HTML page with an embedded list of region names. However, this
is poor and inflexible. If the region database
table changes—that is, new regions are added or deleted or you
want to change a region_name value—you have
to remember to update the HTML page. Moreover, a spelling mistake or
an extra space when creating the HTML page renders a
<select> option useless, because it no
longer matches the values in the database when used for querying. A
better approach is to use the techniques from Chapter 4 to dynamically query the database and produce
a <select>
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access