3 Replies - 3011 Views - Last Post: 17 December 2004 - 07:01 AM Rate Topic: -----

#1 mindytruitt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-August 04

Checking Exsistance Of A Variable?

Post icon  Posted 10 August 2004 - 03:07 PM

Is is possible to check the exsistance of a variable?

I am wanting to use checkboxes in a form, but I know that if it is not checked then the variable is not even created. Is there anyway to see if a variable was created or not?
Is This A Good Question/Topic? 0
  • +

Replies To: Checking Exsistance Of A Variable?

#2 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1868
  • View blog
  • Posts: 20,279
  • Joined: 17-March 01

Re: Checking Exsistance Of A Variable?

Posted 10 August 2004 - 03:08 PM

I'm a fan of:

<cfif isDefined('form.fieldname') AND form.fieldname NEQ "">

</cfif>
Was This Post Helpful? 0
  • +
  • -

#3 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1868
  • View blog
  • Posts: 20,279
  • Joined: 17-March 01

Re: Checking Exsistance Of A Variable?

Posted 10 August 2004 - 03:11 PM

As a side note, I use that on ALL if statements, if the variable isn't defined, it will stop there, not checking whether it's blank or not, this prevents errors from non-defined variables.
Was This Post Helpful? 0
  • +
  • -

#4 SpongeC  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-December 04

Re: Checking Exsistance Of A Variable?

Posted 17 December 2004 - 07:01 AM

skyhawk133, on Aug 10 2004, 03:08 PM, said:

I'm a fan of:

<cfif isDefined('form.fieldname') AND form.fieldname NEQ "">

</cfif>

Instead of that you could do:

<cfparam name="form.MyCheckbox" type="string" default="">
<cfif len(form.MyCheckbox)>

</cfif>


The advantages are:
-After you use cfparam you know that your variable (form.MyCheckbox) will always have a value. The value could be an empty string if the checkbox is not checked or the value "on" if it is checked.
-You can initialise a value. Suppose that you want a checkbox to be by default checked. You can set the cfparam to be "on" and in your if statement set the checkbox to checked.

My advice is when you can avoid using isDefined, then you should!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1