Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In ColdFusion, how do I determine if a query string variable exists?

Tags:

coldfusion

In ColdFusion, how can I determine if a variable exists within the querystring without throwing an error attempting to check it?

like image 698
George Johnston Avatar asked Feb 25 '10 23:02

George Johnston


2 Answers

There are two options.

The first is to use cfparam to define a default value eg:

<cfparam name="url.varname" type="string" default="" />

This ensures that you can always refer to url.varname

The second is to use isDefined or structKeyExists to test for the presence of the variable:

<cfif isDefined("url.varname") and url.varname eq 42> do something </cfif>

or

<cfif structKeyExists(url, "varname") and url.varname eq 42> do something </cfif>
like image 128
Antony Avatar answered Oct 24 '22 04:10

Antony


I have used this approach in many places.

At the top of the page:

<cfparam name="request.someVal" default="request.defaultVal">

Later in the page or custom tag, check for the value of the request.someVal variable, without fear of it crashing, since it has a default value.

<cfif ("request.someVal" eq "something")>
    ...
</cfif>
.
.
.
like image 38
jamesTheProgrammer Avatar answered Oct 24 '22 04:10

jamesTheProgrammer