Create Dynamic Checkboxes in Coldfusion

I am trying to create check boxes from a query. The query simply pulls the name and id number of all current employees and creates the check boxes with those values. The user then submits the form where the checked values are used to run a query. The problem I am having is checking for which check boxes were checked when I do not know what the names of the check boxes will. I appreciate any examples that you have.
Robcarter10Asked:
Who is Participating?
 
devilJinKazamaCommented:
you should assign the IDs to the form fields and process them on your submit

<cfquery name="q" datasource="foo">
SELECT
    name, ID
FROM
    employees
</cfquery>

<form>
<cfoutput query="q">
    <input type="checkbox" name="employee" value="#ID#"> #name#<br/>
</cfoutput>
<input type="submit" value="submit"/>
</form>

--process section you can run queries like so

<cfquery name="updateq" datasource="foo">
UPDATE
    employees
SET
    deleted = 1
WHERE
    employeeID IN (#form.employee#)
</cfquery>

Open in new window


hope that helps
0
 
_agx_Commented:
I'm a little confused. Once you generate the checkboxes, what do you need to check or validate - and why?  What does your query look like?
0
 
_agx_Commented:
>  employeeID IN (#form.employee#)

Just be sure to use cfqueryparam, because that query is vulnerable to sql injection

ie Instead use

WHERE  employeeID IN
(
        <cfqueryparam value="#form.employee#" cfsqltype="cf_sql_integer" list="true">
)
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
devilJinKazamaCommented:
ah yes good point, cheers mate
0
 
gdemariaCommented:
(no points)

Just a little clarification, it wasn't quite said... but demonstrated.

You should name all the checkboxes with the same name, give each one the value of the ID... the result will be a form variable with a comma delimited list of IDs...

   <input type="checkbox" name="employee" value="#ID#"> #name#<br/>

 Form.ID =   23,34,59,67,102

0
 
ZvonkoSystems architectCommented:
Here an checkbox example:
<cfquery name="myEmploy" DATASOURCE="eeDSN" >
  SELECT employeeNumber, employeeName, employeeSelected FROM EmployTab
</cfquery>

<cfform name="myForm" method="POST" preserveData="yes" >
<cfoutput>
<cfloop query="myEmploy" >
  <cfinput type="checkbox" name="employee_#employeeNumber#" value="#employeeNumber#" CHECKED="#employeeSelected#" > #employeeName# <br>
  <cfif isDefined("form.employee_#employeeNumber#") >
     <cfquery name="setState" DATASOURCE="eeDSN" >
       UPDATE EmployTab SET employeeSelected=1 WHERE employeeNumber=#employeeNumber#
     </cfquery>
  <cfelseif isDefined("FORM.FIELDNAMES") >
      <cfquery name="setState" DATASOURCE="eeDSN" >
       UPDATE EmployTab SET employeeSelected=0 WHERE employeeNumber=#employeeNumber#
     </cfquery> 
  </cfif>
</cfloop>
</cfoutput>
<input type="submit" name="doUpdate" value="Update">
</cfform>

<cfquery name="myEmploy" DATASOURCE="eeDSN" >
  SELECT employeeNumber, employeeName, employeeSelected FROM EmployTab
</cfquery>
<cfdump var="#myEmploy#">

Open in new window

0
 
ZvonkoSystems architectCommented:
Sorry better example is this one:
<cfquery name="myEmploy" DATASOURCE="eeDSN" >
  SELECT employeeNumber, employeeName, employeeSelected FROM EmployTab
</cfquery>

<cfform name="myForm" method="POST" preserveData="yes" >
<cfoutput>
<cfloop query="myEmploy" >
  <cfinput type="checkbox" name="employee_#employeeNumber#" value="#employeeNumber#" CHECKED="#employeeSelected#" > #employeeName# <br>
  <cfif isDefined("form.employee_#employeeNumber#") >
     <cfif employeeSelected NEQ 1 >
       <cfquery name="setState1" DATASOURCE="eeDSN" >
         UPDATE EmployTab SET employeeSelected=1 WHERE employeeNumber=#employeeNumber#
       </cfquery>
     </cfif>
  <cfelseif isDefined("FORM.FIELDNAMES") AND employeeSelected NEQ 0 >
      <cfquery name="setState0" DATASOURCE="eeDSN" >
       UPDATE EmployTab SET employeeSelected=0 WHERE employeeNumber=#employeeNumber#
     </cfquery> 
  </cfif>
</cfloop>
</cfoutput>
<input type="submit" name="doUpdate" value="Update">
</cfform>

<cfquery name="myEmploy" DATASOURCE="eeDSN" >
  SELECT employeeNumber, employeeName, employeeSelected FROM EmployTab
</cfquery>
<cfdump var="#myEmploy#">

Open in new window

0
 
_agx_Commented:
> Just a little clarification, it wasn't quite said... but demonstrated.
> You should name all the checkboxes with the same name, ...

Agreed. Assuming we're guessing correctly about your query and your objective that is ;)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.