Robcarter10
asked on
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.
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> 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">
)
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"
)
ah yes good point, cheers mate
(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
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
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#">
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#">
> 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 ;)
> You should name all the checkboxes with the same name, ...
Agreed. Assuming we're guessing correctly about your query and your objective that is ;)