Solved

How to check form checkboxes based on db query

Posted on 2012-04-13
10
280 Views
Last Modified: 2012-04-13
Hi Experts,

FIELD1: I have the following string in a db field: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15

FIELD2: I have another string in another field: 2,3,4,7,10,11,13

I am using FIELD1 with the following code to display 12 checkboxes.  I am displaying a page that lists all FIELD1 numbers as checkboxes.  As the user checks the available checkboxes and submits the form, I am inserting the selected checkboxes into FIELD2.

I am now trying to setup an update web form that will display all available numbers from FIELD1 and check those boxes if they are contained in FIELD2.

I have attached a screen shot, so I hope that helps.

Also, I have attached my code, since I am having trouble displaying it here in the comments... I am using the cfloop through FIELD1 to display all the checkboxes.


Comment: The database field is in the following format 1,2,3,4,5 and I am using numberformat to display at 001,002,003,004 ect...
all-available-checkboxes.PNG
previously-selected-checkboxes.PNG
code-sample.txt
0
Comment
Question by:ehart12
  • 5
  • 5
10 Comments
 
LVL 52

Expert Comment

by:_agx_
ID: 37844181
Can you post the structure of your 2 tables?  Because I'm not clear on whether the values are being stored in separate rows or as a big delimited "string"

ie   Row  | Value
      #1    |  2
      #2    | 3
      #3    | 4

OR

      #1    |  "2,3,4"
0
 

Author Comment

by:ehart12
ID: 37844202
The actual strings are stored in two seperate tables and look like this;

Table#1 | 1,2,3,4,5,6

Table#2 | 2,3,4

Table#1contains all the available options
Table#2contains only the selected options from Table#1

Does that help?
0
 
LVL 52

Expert Comment

by:_agx_
ID: 37844228
But are the values in the same row

                       SomeColumn
        Row #1   2,3,4

or each in a separate row?

                       SomeColumn
        Row #1   2
        Row #2   3
        Row #3   4

If they're all in one row, any chance you can change the structure? Because storing lists isn't recommended (.. and tasks like this are exactly why :) .
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

Author Comment

by:ehart12
ID: 37844308
Oh no, that does not sound good...  

All of the numbers are stored in a single row  (1,2,3,4,5...) and I have no flexability to change the field format.

I could take the string of numbers (1,2,3,4,5...) and break them apart and insert them each into their own row in a temporary table. Would that make what I am asking for any easier?
0
 
LVL 52

Expert Comment

by:_agx_
ID: 37844386
and I have no flexibility to change the field format.

Ok, lets just try and work with what you have.  (But going forward storing lists violates good db design rules. It's usually the wrong way to go 99% of the time. )

Anyway back to your query. I think I'm still missing something. If #query.serial_number[row]# contains a list of values ie "1,2,3,4"  how is this code generating one checkbox per value?

<cfloop from="1" to="#query.recordcount#" index="x">
      <cfoutput>
            <input type="checkbox" value="#query.serial_number[x]#" name="cbox2" onclick="countCheckboxes()">
....

It should be an easy solution, I'm just not seeing where these "lists" of values come in :)
0
 

Author Comment

by:ehart12
ID: 37844481
Here is the complete code that i use to list all of the checkboxes, one checkbox for each number in the list.

Example: The list string of 1,2,3,4,5 would give me 5 checkboxes

<cfloop from="1" to="#query.recordcount#" index="x">
<cfoutput>
<input type="checkbox" value="#query.serial_number[x]#" name="cbox2" onclick="countCheckboxes()">
SN#numberformat(query.serial_number[x], 000)#
<cfif i lt query.recordcount>,</cfif>
</cfoutput>
</cfif>
</cfloop>
0
 

Author Comment

by:ehart12
ID: 37844487
When the form is submitted, the variable #cbox2# lists all the checkboxes in the 1,2,3,4,5... format.  As you know, if a checkbox is not checked, it is undefined and not sent with the form.
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 37844556
Ok, then your query column doesn't really contain a list.  I think we just crossed wires on that.

Anyway, if you want to compare the loop value against your list of previously checked values, do this:

           <!--- simulate your list --->
           <cfset previouslyCheckedValues = "2,3,4,7,10,11,13">

          <cfloop from="1" to="#query.recordcount#" index="x">
                <cfoutput>
                 <input type="checkbox" value="#query.serial_number[x]#"
                           name="cbox2"
                           onclick="countCheckboxes()"
                         <cfif listFindNoCase( previouslyCheckedValues, query.serial_number[x])>checked</cfif>
                 />
                  SN#numberformat(query.serial_number[x], 000)#
                  <cfif i lt query.recordcount>,</cfif>
             </cfoutput>
        </cfloop>
0
 

Author Closing Comment

by:ehart12
ID: 37844760
That worked perfectly! You had me worried for a second ;)

Thank you!
0
 
LVL 52

Expert Comment

by:_agx_
ID: 37844768
Lol.. so did you with the "lists" ;-) But once I realized "field1" wasn't actually a list (doh), the solution was more workable :)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

856 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