Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

cfif IsDefined

New Here ,
Apr 05, 2007 Apr 05, 2007
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>
TOPICS
Getting started
4.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Apr 06, 2007 Apr 06, 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...
Translate
LEGEND ,
Apr 05, 2007 Apr 05, 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 05, 2007 Apr 05, 2007
It translates to:

If the variable is defined, set a cookie that contains that variable.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 05, 2007 Apr 05, 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 06, 2007 Apr 06, 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?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 06, 2007 Apr 06, 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>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 19, 2007 Apr 19, 2007
Thank you

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 19, 2007 Apr 19, 2007
LATEST
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 ''>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources