Skip to main content
September 17, 2008
Answered

Checking dynamically named checkboxes.

  • September 17, 2008
  • 3 replies
  • 413 views
I have a form with checkbox fields "Check1", "Check2", "Check3, etc. After submit, I want to loop through to find which are checked. Something like:

cfloop index="1" from="1" to="4"

cfif parameterexists (check#i#)
do stuff
/cfif

/cfloop

Except CF isn't happy with this.
(Yes, I know parameterexists has been deprecated. That's not the problem.)

Do you do this with "evaluate"? What is the right cfif command? Help would be appreciated!

thanks!
B.
This topic has been closed for replies.
Correct answer Dan_Bracuk
This sort of thing is almost always simpler if you give all your checkboxes the same name but different values. Then you either have a list or nothing at all. You can use isDefined("form.nameofcheckbox") to check.

3 replies

Inspiring
September 17, 2008
use "coldfusion list functions" for your google search string.
Inspiring
September 17, 2008
BrettCPI wrote:

> Do you do this with "evaluate"? What is the right cfif command? Help would
> be appreciated!
>


If you do not go with Dan's suggestion and a single checkbox name with a
list of values you would use array notation and|or structKey exists to
work with this type of data.

<cfif structkKeyExists("form","Check" & i)>
<cfoutput>#form["check" & i]#</cfoutput>
</cfif>
Dan_BracukCorrect answer
Inspiring
September 17, 2008
This sort of thing is almost always simpler if you give all your checkboxes the same name but different values. Then you either have a list or nothing at all. You can use isDefined("form.nameofcheckbox") to check.
September 17, 2008
Why didn't I think of that? Thanks, Dan!

You wouldn't happen to have the cfif statement to check the box for a certain value handy? I'll google it!