Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Coldfusion checking if variable has a value

Posted on 2011-02-26
8
Medium Priority
?
357 Views
Last Modified: 2012-05-11
Hi,
I need to check if a variable has a value in it. If it does, display out, if not, ignore it.

I tried this:
<cfif #name#>
Dear #name#,<br/>
</cfif>

The response I got back was that value "Mike Smith" can't be converted to boolean.
What is the correct syntax for checking something like this?

Thanks!

0
Comment
Question by:nachtmsk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 34987646
<cfif isDefined(name) and name NEQ ''>
         Dear #name#,<br/>
</cfif>

Open in new window

0
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 34987661
try this, it needs to be enclosed in string..

http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7fbd.html

<cfif isDefined("#name#") and #name# NEQ ''>
         Dear #name#,<br/>
</cfif> 

Open in new window

0
 
LVL 39

Expert Comment

by:gdemaria
ID: 34987799

Neither of those is correct, you need to put quotes around the variable name.  The variable name must be in quotes because it might not exists.

<cfif isDefined("name")>
  Dear #name#,<br/>
</cfif>
0
A new era in Cloud training has arrived.

A day that will go down in Cloud history.. But are you ready for it? Will you accept this Cloud challenge?

 
LVL 39

Expert Comment

by:gdemaria
ID: 34987816

But what if the value is empty?   I don't think you want to say   Dear  ,  

So you want to test if it exists and if it is empty...

<cfif isDefined("name") and len(name)>
   Dear #name#,
</cfif>



One more method is to just create the variable if it doesn't exist using cfparam,  that will allow you to use it for the rest of the page without always testing if it exists...

<cfparam name="name" default="">

<cfif len(name)>
   Dear #name#,
</cfif>


0
 
LVL 11

Accepted Solution

by:
Brijesh Chauhan earned 2000 total points
ID: 34987862
okay the idea is to use isDefined function to check the existence of variable, thanks for correcting the syntax.

you can define name and then check if it is not blank or not

<cfparam name="name" default="" />

<cfif isDefined("name") and #name# NEQ ''>
         Dear #name#,<br/>
</cfif>

Open in new window

0
 
LVL 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 34989980
I am bit late to come here, All the syntax specified by gd and brij are correct and you can use any one and it will work!

Like this:

<cfparam name="name" default="">

you can do the following!

<cfset isName = isDefined('name') and name NEQ ''>


the isName variable with return you the value yes/no in this case and ur purpose solved and this is reusable if you want to use it in multiple scenarios in page


<cfif isName>
Welcome #name#
<cfelse>
Welcome Yourname
</cfif>
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 34990037
<cfparam name="name" default="" />  <!---- this makes sure the variable exists

<cfif isDefined("name") and #name# NEQ ''>  <!------ there is no point in using IsDefined() here because you KNOW the variable exists from previous line, also there is no need for the #s in the CFIF statement
         Dear #name#,<br/>
</cfif>


>  All the syntax specified by gd and brij are correct

NOT TRUE.  The ones in brij's first posts will cause an error and are not correct

0
 
LVL 39

Expert Comment

by:gdemaria
ID: 35096054
Did you choose that answer randomly?

You don't need to check if a variable is define if you just used cfparam to ensure it exists


<cfparam name="name" default="" />  <!===== you define it here

<cfif isDefined("name") and #name# NEQ ''>  <!==== here you check to see if it's defined
      Dear #name#,<br/>
</cfif>


<cfparam name="name" default="" />  
<cfif len(name)>  <!==== don't use # inside a cf statement, len() is faster than NEQ
  Dear #name#,<br/>
</cfif>

Open in new window

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question