Tricks with Naming Form Fields In UltraDev
by
Tom Muck
Many people don't realize that you can name your form fields
with the same name. What happens when you do this? You'll get a comma-delimited
list of values that you can retrieve on the action page. As an example
of what I mean, take a look at these checkboxes:
If you click the submit button, you'll see the result below:
You answered:
The code used to display the result is simply:
<%=Request("language")%>
That works because all of the checkboxes are named language,
and the checked values are given as ASP, JSP, CF, and PHP.
Making a Batch Delete Page
This can come in handy in a multiple-record delete page
as well. For example, you could put a repeat region on the page displaying
records that have a checkbox next to them. All you have to do is to put
a checkbox in the Repeat Region or Horizontal Looper along with the fields
you are displaying, and then post the form to a Delete page.
The checkbox value should be set to the primary key of the
record. You can do this one of two ways:
- Dragging the field from the Data Bindings palette and
dropping it on the checkbox
- Select the checkbox on the page, select the field in
the Data Bindings palette, and click the Bind button.
On that delete page you can put a command with a SQL statement
like:
Delete from MyTable where MyID IN (MyCheckbox)
Then you need to set up a variable named MyCheckbox in the
command dialog box and make the value:
ASP |
Request("checkbox") |
JSP * |
request.getParameter("checkbox") |
CF |
#Form.checkbox# |
PHP |
$checkbox |
When you browse the page, there will be a checkbox named
checkbox next to each record in the Repeat Region. Each checkbox will
have a different value in it -- the value of the ID number of that particular
record. When you click submit, the Request value retrieved from the action
page will be a comma-delimited list of all ID values from the checked
boxes. The SQL statement uses the IN keyword to delete all records that
are in that comma-delimited list.
*Note: JSP users may need to use getParameterValues()
Using Other Form Fields
The technique is not only for checkboxes -- it works with
any form field. For example, you can have text fields on a page just as
easily:
If you click submit, you can see your search string here:
You are searching for:
The code for displaying the result was simply:
<%=Request("search")%>
As you can see, with other form fields the technique works
a little differently. If a field is left blank, there is an empty value
in the comma separated list. This can still be used in a variety of different
situations. A simple Split method in VBScript will populate an array with
the values returned -- leaving blank values where nothing was entered.
ColdFusion users have a variety of different functions available for working
with comma-separated lists.
Using a multi-line listbox is just as easy. You can put
a multi-line listbox on the page, and any values that are selected will
be returned as a comma-separated list:
You chose:
Once again, the only server-side code used to display the
result is:
<%=Request("listdemo")%>
The listbox returns only the selected values, like
the checkboxes in the example above.
There are certainly many uses for this technique. This article
was meant only as a brief introduction intended to get you to think about
the process.
Tom Muck
|