Solved

Coldfusion checking if variable has a value

Posted on 2011-02-26
8
353 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
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 
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 500 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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

636 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