|
|
|

Dealing With Empty Variable Names In ColdFusion
This
is the long awaited ColdFusion article that has kept many
of you on your seats for the past few months, as I was
either too busy to write this article, or had more
important information to get to you as time drifted by.
Therefore, to recap the subjects that we have written
about in the past,
we have covered the topic of how to get started using
ColdFusion here at the University of North Texas,
Creating your first application using ColdFusion, and
Connecting to a database using ColdFusion. We, therefore,
have a good basic foundation on which to draw from to
continue in our path down web application creation lane.
Now it is time to look at an oft overlooked, and very
under-documented occurrence of an empty parameter. How do
you deal with an empty or non-existent variable that is
passed to the evaluating .cfm page?Its easy!!
In this article, we are going to show you
how to deal with offending empty variables, and bring
about smooth, elegant applications using ColdFusion.
To Begin
To start, we must have our beginning
html page, named testParam.html, that will hold two forms
named: name and phone.
- <HTML>
- <HEAD>
-
<TITLE>Welcome to the Test</TITLE>
- </HEAD>
- <BODY>
- <P><FONT SIZE=4 STYLE="font-size:
16pt">
-
<B>Welcome to the ColdFusion Test
Application:</B></FONT></P>
- <FORM ACTION="testParam2.cfm"
METHOD="post">
-
<P><FONT SIZE=3><B> Please
enter your name here:
-
<INPUT TYPE=TEXT NAME="name"
SIZE=25></B></FONT></P>
-
<P><FONT SIZE=3><B> Please
enter your telephone here:
-
<INPUT TYPE=TEXT NAME="phone"
SIZE=25></B></FONT></P>
-
<DIV ALIGN=LEFT>
-
<P><FONT SIZE=3><B> <INPUT
TYPE=BUTTON
NAME="submit"
VALUE="Submit"></B></FONT></P>
-
</DIV>
- </FORM>
- </BODY>
- </HTML>
Many of you will recognize this from
the first application article in this series. This is a
simple form that accepts input into two text boxes, and
sends that input to a file named testParam2.cfm. In
testParam2.cfm, we are going to use the existing code
that we created in the first app article, which looks
like this.
- <HTML>
- <HEAD>
- <TITLE>This page interprets the data from
test.cfm </TITLE>
- </HEAD>
- <BODY>
- <CFOUTPUT>
-
<H2># Form.name #</H2>
-
<H2># Form.phone
#</H2>
- </CFOUTPUT>
- </BODY>
- </HTML>
This
page outputs the data held in the variables form.name and
form.phone to the browser. This is a nice simple script
to get started with, but what happens if we add a
checkbox to the first page and try to interpret the data
on the next page? Well
Lets try it. Add this
code beneath your text boxes on testParam.html.
<p>
<FONT SIZE=3><B> Wasnt this the
greatest page? (Check if you like the
page) <input type="checkbox"
name="likePage" value="1">
This
html adds a checkbox to the bottom of our page which asks
the user to tell us if they liked the page or not
As you know, if they mark the checkbox, then the page
will send forward a variable that can be accessed with
#form.likePage# from the testParam2.cfm page. Like
this:
- <HTML>
- <HEAD>
- <TITLE>This page interprets the data from
test.cfm </TITLE>
- </HEAD>
- <BODY>
- <CFOUTPUT>
-
<H2>#Form.name#</H2>
-
<H2>#Form.phone#</H2>
-
<H2>#Form.likePage#</H2>
- </CFOUTPUT>\
- </BODY>
- </HTML>
If
the checkbox is marked, you will get a returned value of
1, because that is the value that we have set the
checkbox equal to if checked. But
What if its
not checked? Will the testParam.html page send forward a
variable value of 0? Of course not. HTML is not
intelligent, and will only send forward the variables
that hold values. We will then get an error when the user
clicks on the submit button at the bottom of the page.
How
do we deal with this?
The
trick to handling this tricky situation is to introduce
the <CFPARAM> tag. The <CFPARAM> tag is
ColdFusions handy way of test(ing) for a
parameters existence and optionally provide a
default if it is not found[1]The
way to use the <CFPARAM> tag is to place it at the
top of your page, before the <HTML> tag, and create
a default for the parameter that you are looking for.
Here is the syntax diagram for a <CFPARAM>
statement:
- <CFPARAM NAME="Form.check_name"
default="">
Therefore,
our <CFPARAM> tag will look like this:
- <CFPARAM NAME=Form.likePage
default=0>
The
reason that I am setting a default of zero, is because it
is logical to test for 0, no, the user doesnt like
the page, or 1, yes, the user did like the page. (In the
world of Boolean datatypes, 1 equals true, and 0 equals
false.) Therefore, if the user does not like our
wonderful page, they will click submit, sending forward a
value for the Form.name and Form.phone variables, and
nothing at all for our Form.likePage variable. Our
testParam2.cfm page receives this data, and is sent to
the ColdFusion server for parsing, where the first
statement of the page checks for a non-existing value for
Form.likePage, and then, because the value doesnt
exist, creates a value for Form.likePage=0.
The rest of the page parses, and the values are then
output to the browser. Here is the final code:
- <CFPARAM NAME=Form.likePage
default=0>
- <HTML>
- <HEAD>
- <TITLE>This page interprets the data from
test.cfm </TITLE>
- </HEAD>
- <BODY>
- <CFOUTPUT>
-
<H2>#Form.name#</H2>
-
<H2>#Form.phone#</H2>
-
<H2>#Form.likePage#</H2>
- </CFOUTPUT>
- </BODY>
- </HTML>
Its
that easy!
That
is the quick and easy answer to a troubling, and oft
ignored question of non-existing parameters, which would
have stopped our ColdFusion application dead in its
tracks. With the use of the <CFPARAM> tags, we can
check for any number of non-existing parameters from the
sending page, and then test that value for
flow-of-control purposes. That being the case, please
wait with baited breathe until the next installment which
will begin our journey down the first true programming
concept of ColdFusion. Control Structures! Until
then
Live long and prosper!
Shannon
Peevey Central Web Support
|