Help - Search - Members - Calendar
Full Version: Variables in CFIF
W3Schools Forum > Server Scripting > ColdFusion
vchris
CODE
<cfif isDefined('form.submitExtraReg')>
    <cfif form.extra neq 0>
        <cfloop index="i" from="1" to="#form.extra#">
            <cfif len(form.extraName_#i#) gt 0 AND len(form.extraEmail_i) gt 0 AND len(form.extraPhone_i) gt 0>
                Success
            </cfif>
        </cfloop>
    </cfif>
</cfif>

I have a loop that creates up to 8 rows of 3 inputs depending on the choice of a drop down. I need to validate these text input. I want to validate them with a cfloop since I don't know how many there could be. Each input is named with a _# (_1, _2, _3) that indicates the row they are on. It seems I get an error with the above code. CF says the # inside the len is invalid. If I just leave it to _i, it doesn't evaluate it. Is there a way to have it evaluate i?

Error
QUOTE
Invalid CFML construct found on line 69 at column 50.
ColdFusion was looking at the following text:<p>#</p><p>The CFML compiler was processing:<ul><li>an expression beginning with "len", on line 69, column 31.This message is usually caused by a problem in the expressions structure.<li>a cfif tag beginning on line 69, column 26.<li>a cfif tag beginning on line 69, column 26.<li>a cfif tag beginning on line 69, column 26.<li>a cfif tag beginning on line 69, column 26.</ul>

<cfif len(form.extraName_#i#) gt 0 AND len(form.extraEmail_i) gt 0 AND len(form.extraPhone_i) gt 0>
Skemcin
In these cases, I often try to set and reset variables:
CODE
<cfloop index="i" from="1" to="#form.extra#">
<cfset xname=evaluate(form.extraName_#i#)>
<cfset xname=len(xname)>
You might have to rewrite some other logic, but breaking it up can often make it easier.


FYI
As a side note, overall performance is faster by omitting the # in ColdFusion tags when possible.
For instance:
<cfset xname=len(xname)>
instead of
<cfset xname="#len(xname)#">
vchris
I get " Invalid CFML construct" because of the #i# inside the cfset...

EDIT: Solved.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.