Skip to main content
October 30, 2012
Answered

cfloop issue cf9 to cf10

  • October 30, 2012
  • 2 replies
  • 1436 views

i have a loop setup on an insert that was running fine on cf9, but on cf10 it is erroring with an "expressions error"

code is:

<cfloop index="x" from="1" to="#cc#" step="1">

<cfif IsDefined("FORM.["Interest" & #x#]") AND FORM.["Interest" & #x#] is 1>

    1

      <cfelse>

      0

  </cfif>

  ,

  </cfloop>

it is looping through a form field "Interest1, Interest2, Interest3", etc

cc is the number of form fields

any suggestions appreciated.

thanks.

This topic has been closed for replies.
Correct answer Adam Cameron.

i have a loop setup on an insert that was running fine on cf9, but on cf10 it is erroring with an "expressions error"

code is:

<cfloop index="x" from="1" to="#cc#" step="1">

<cfif IsDefined("FORM.["Interest" & #x#]") AND FORM.["Interest" & #x#] is 1>

 

Sorry, but that code you specify there will not run on any version of ColdFusion.

Firstly you have a syntax error in your variable referencing as others have pointed out.  This is invalid:

FORM.["Interest" & #x#]

It needs to be this:

FORM["Interest" & #x#]

(ie: without the dot).

Secondly, isDefined() only works with dot-notation variable names, eg: form.interest1 etc. It cannot be used with associative array syntax or any other sort of notation.  so - syntax errors aside - this will not work:

IsDefined("FORM.["Interest" & #x#]")

(oh... a third error... if you're using double quotes as the string delimiter, and have embedded double quotes in the string, you need to escape them, eg "".  However that is neither here nor there in this situation, as the code is broken in two other ways anyhow.

So you're giving us misleading information there.

What you want to be using is this:

structKeyExists(form, "Interest#x#") and form["Interest#x#"]

Although using the & operator will work fine there too, instead of the inline variable resolution.

Avoid isDefined() altogether if poss, as it's a bit old and creaky and gives false positives compared to using structKeyExists(), which just works.

--

Adam

2 replies

Adam Cameron.Correct answer
Inspiring
October 30, 2012

i have a loop setup on an insert that was running fine on cf9, but on cf10 it is erroring with an "expressions error"

code is:

<cfloop index="x" from="1" to="#cc#" step="1">

<cfif IsDefined("FORM.["Interest" & #x#]") AND FORM.["Interest" & #x#] is 1>

 

Sorry, but that code you specify there will not run on any version of ColdFusion.

Firstly you have a syntax error in your variable referencing as others have pointed out.  This is invalid:

FORM.["Interest" & #x#]

It needs to be this:

FORM["Interest" & #x#]

(ie: without the dot).

Secondly, isDefined() only works with dot-notation variable names, eg: form.interest1 etc. It cannot be used with associative array syntax or any other sort of notation.  so - syntax errors aside - this will not work:

IsDefined("FORM.["Interest" & #x#]")

(oh... a third error... if you're using double quotes as the string delimiter, and have embedded double quotes in the string, you need to escape them, eg "".  However that is neither here nor there in this situation, as the code is broken in two other ways anyhow.

So you're giving us misleading information there.

What you want to be using is this:

structKeyExists(form, "Interest#x#") and form["Interest#x#"]

Although using the & operator will work fine there too, instead of the inline variable resolution.

Avoid isDefined() altogether if poss, as it's a bit old and creaky and gives false positives compared to using structKeyExists(), which just works.

--

Adam

October 30, 2012

Thanks for the help. Got it working now.

Inspiring
October 30, 2012

When I use array notation, I don't have a period between the structure and the opening square bracket.  Given that CF tends to be less forgiving of those sorts of imperfections as the versions get higher, that might be the problem.

October 30, 2012

thanks, but isn't the period necessary to separate the input type from the name of the form, e.g. FORM.Interest1 vs FORMInterest1

If written without the loop, it would be:

<cfif IsDefined("FORM.Interest1") AND FORM.Interest1 is 1>

    1

      <cfelse>

      0

  </cfif>

  ,

<cfif IsDefined("FORM.Interest2") AND FORM.Interest2 is 1>

    1

      <cfelse>

      0

  </cfif>

  ,

<cfif IsDefined("FORM.Interest3") AND FORM.Interest3 is 1>

    1

      <cfelse>

      0

  </cfif>

  ,

WolfShade
Legend
October 30, 2012

form["interest"&x] is, I believe, the proper notation.

^_^