Solved

Coldfusion checking if variable has a value

Posted on 2011-02-26
8
345 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
  • 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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 15

Expert Comment

by:myselfrandhawa
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now