Skip to main content
Known Participant
April 5, 2007
Answered

cfif IsDefined

  • April 5, 2007
  • 4 replies
  • 3921 views
Hello still going through the basics .
there are several cfif IsDefined

what is it doing in this code?

thanks



<cfif IsDefined("URL.CompanyID")>
<cfcookie name="CompanyID" value="#URL.CompanyID#">
</cfif>

<cfif IsDefined("URL.ShowAll")>
<cfcookie name="CompanyID" expires="NOW">
</cfif>

<cfquery name="GetEmployees"
datasource="#Request.MainDSN#">
SELECT
c.CompanyName,
e.SSN,
e.Firstname,
e.Lastname,
e.Salary,
e.DateOfBirth
FROM
Employee e INNER JOIN Company c
ON e.CompanyID = c.CompanyID
<cfif IsDefined("Cookie.CompanyID") AND Len(Trim(Cookie.CompanyID))>
WHERE
e.CompanyID = #Val(Cookie.CompanyID)#
</cfif>
ORDER BY
c.CompanyName,
e.Lastname,
e.Firstname
</cfquery>

<html>
<head>
<title>ColdFusion MX Bible</title>
<link rel="stylesheet" href="styles.css">
</head>

<body>

<h1>Employee List</h1>

<table>
<tr>
<td><b>Company</b></td>
<td><b>SSN</b></td>
<td><b>Name</b></td>
<td><b>Salary</b></td>
<td><b>DOB</b></td>
<td> </td>
</tr>

<cfoutput query="GetEmployees">
<tr>
<td>#CompanyName#</td>
<td>#SSN#</td>
<td>#Lastname#, #Firstname#</td>
<td>#Salary#</td>
<td>#DateFormat(DateOfBirth, "mm/dd/yyyy")#</td>
<td>
<a href="EmployeeAddForm.cfm">Add</a>
<a href="EmployeeEditForm.cfm?SSN=#URLEncodedFormat(Trim(SSN))#">Edit</a>
<a href="EmployeeDeleteForm.cfm?SSN=#URLEncodedFormat(Trim(SSN))#">Delete</a>
</td>
</tr>
</cfoutput>

</table>

</body>
</html>
This topic has been closed for replies.
Correct answer craigkaminsky
You don't have to use IsDefined() all the time. You use it when you need to check if a variable exists. The most common uses, I believe, are testing for the existence of FORM and URL variables (this is certainly not all and, as you continue to use CF, you'll find lots of other places it helps).

In other words, you can't be sure that such (URL and FORM vars in this example) variables will exist when a CFM template that requires them is accessed.

For example, if I have a CFM template that looks up users based on an email address that someone enters into a form field, I need to make sure that the email was provided. For example:
<cfif IsDefined("FORM.email")> // the sql cannot be run without this variable, so we test for it. If it exists, we run the query
<cfquery name="qrySample" datasource="someDB">
select * from users where email = '#Trim(FORM.email)#'
</cfquery>
<cfelse>
<p>An email address is required to blah blah blah</p> // error note to user if the email variable is not defined
</cfif>

4 replies

craigkaminskyCorrect answer
Inspiring
April 6, 2007
You don't have to use IsDefined() all the time. You use it when you need to check if a variable exists. The most common uses, I believe, are testing for the existence of FORM and URL variables (this is certainly not all and, as you continue to use CF, you'll find lots of other places it helps).

In other words, you can't be sure that such (URL and FORM vars in this example) variables will exist when a CFM template that requires them is accessed.

For example, if I have a CFM template that looks up users based on an email address that someone enters into a form field, I need to make sure that the email was provided. For example:
<cfif IsDefined("FORM.email")> // the sql cannot be run without this variable, so we test for it. If it exists, we run the query
<cfquery name="qrySample" datasource="someDB">
select * from users where email = '#Trim(FORM.email)#'
</cfquery>
<cfelse>
<p>An email address is required to blah blah blah</p> // error note to user if the email variable is not defined
</cfif>
Known Participant
April 19, 2007
Thank you

Inspiring
April 19, 2007
Another good idea when checking for the existance of a variable is to also ensure the variable actually contains what you would expect by combining other functions such as trim(), IsNumeric(), IsDate() etc. into the check:

cfif IsDefined("FORM.email") and Trim(FORM.email) neq ''>
April 5, 2007
If you attempt to access a variable that is not defined, you will get an error.

Your code is simply checking that the variable exists, using the IsDefined function, before it attempts to access the variable.
Known Participant
April 6, 2007
everytime you have a variable, do you have to use is defined to see if the variable exist?

what if you declare your own variable?
do you have to do usse is defined?
tclaremont
Inspiring
April 5, 2007
It translates to:

If the variable is defined, set a cookie that contains that variable.
Inspiring
April 5, 2007
start by readin about isDefined() function in cf reference. you can
download it here: http://www.adobe.com/support/documentation/en/coldfusion/

--

Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com