Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Create Dynamic Checkboxes in Coldfusion

Posted on 2011-10-18
8
Medium Priority
?
476 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:Robcarter10
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 52

Expert Comment

by:_agx_
ID: 36989531
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
 
LVL 3

Accepted Solution

by:
devilJinKazama earned 2000 total points
ID: 36989539
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
 
LVL 52

Expert Comment

by:_agx_
ID: 36989561
>  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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 3

Expert Comment

by:devilJinKazama
ID: 36989781
ah yes good point, cheers mate
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 36990333
(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
 
LVL 63

Expert Comment

by:Zvonko
ID: 36990688
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 36990712
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
 
LVL 52

Expert Comment

by:_agx_
ID: 36992329
> 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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month20 days, 21 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question