Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic checkboxes

Status
Not open for further replies.

hpvic03

Technical User
Aug 2, 2006
89
0
0
I have a page that generates a form with dynamically named checkboxes. I need a way to figure out which check boxes were checked. Anyone know of a way to do this?

Just think of a simple database of names:

John
Joe
Frank
Fred
Alex

Each name has a checkbox next to it. There is a submit form at the bottom. Which names were checked?

The checkboxes are named after each person. So I thought of just looping through the database to see if each name was checked,

<cfoutput query="namelist">
<cfif form.#name# is "checked"> -- code -- </cfif>
</cfoutput>

But apparently you can't do form.#name#. So I don't know where to go from there. Thanks in advance!
 
Checkboxes only exist on the action page if they were checked. So you could use IsDefined(). If the field is defined, that means it was checked.

<cfoutput query="namelist">
<cfif IsDefined("form.#name#")>
-- code --
</cfif>
</cfoutput>

You can also reference dynamic fields using array notation:

<cfoutput query="namelist">
The value = #form[name]#<br />
</cfoutput>

As I'm not sure what your form does, I don't know if that's the best method, but it would work.
 
Thanks! I'll give it a try and get back to you. I know for sure that form.#variable# will not work, but the array might work.
 
This will work because the code is only evaluating the value of the query column #name# (not the form field value)

<cfoutput query="namelist">
<cfif IsDefined("form.#name#")>
-- code --
</cfif>
</cfoutput>
 
I just tried both of those things and got an error on each. I can't use the array because when I leave a checkbox out I get this error:

Element 5 is undefined in a Java object of type class coldfusion.filter.FormScope referenced as ...
(5 is the person's id number who was left unchecked, also the name of the checkbox)

And when I did <cfif isdefined("form.#name#")> I got this error:

Parameter 1 of function IsDefined, which is now "form.4", must be a syntactically valid variable name.
(4 was the first person's id which came up first in the query)

Any more ideas? I can always change the name of the checkbox and it's value if that will bring about another solution. Please let me know. Thanks!
 
Actually I just got it to work with the quotes. Thank you!
 
really check boxes were meant to store multiple values.
so you'd have several check boxes with with the same name, different values.

Code:
<cfoutput query = "nameList">
  <input type = "checkbox" name = "names" value = "#nameList.name#" <cfif listFind(form.names, nameList.name)>checked = "Checked"</cfif>>#nameList.name#
<br>
</cfoutput>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top