Coldfusion checkbox values from query or value list

Posted on 2009-02-18
Last Modified: 2013-11-19
I'm trying to make sure checkboxes are checked if a corresponding value exists in another query.  I have all of the add/remove stuff working, but getting the checkboxes to display as checked on an information screen is killing me.  Have tried ListGetAt, but couldn't get it to work.  I'll post my code below.

My checkboxes are created using the 'get_animals' query.

I have a second query to check to see if an existing store has that animal selected - 'cust_animals'.

What I want to do is check the box if the current checkbox's value exists in the cust_animals query.
<!--- GET ALL ANIMALS --->

<cfquery name="get_animals" datasource="#DSN#">

       	select * from lookup_animal order by animal_name asc



<cfquery name="cust_animals" datasource="#DSN#">

       	select * from customer_animals where animal_customer_id = ''



<cfoutput query="get_animals">

    <input name="animal_id" type="checkbox" value="#animal_id#" /> #animal_name#<br />


<!--- Now how do it check the box if the animal_id exists in cust_animals? --->

Open in new window

Question by:Newco
    LVL 16

    Expert Comment


    <cfoutput query="get_animals">
    	<cfif ListFindNoCase(ValueList(cust_animals.animal_id), get_animals.animal_id)>
    	<!--- this ID is in the other query --->
    		<input name="animal_id" type="checkbox" value="#animal_id#" checked /> #animal_name#<br />
    	<!--- not found --->
    		<input name="animal_id" type="checkbox" value="#animal_id#" /> #animal_name#<br />

    Open in new window

    LVL 51

    Accepted Solution

    Another option is to use a single query and determine that value using a CASE statement in your query. Most databases like ms sql, mysql, etc... support CASE.  If you are using Access, try IIF instead.
    <!--- GET ALL ANIMALS --->
    <cfquery name="get_animals" datasource="#DSN#">
       select la.animal_id, la.animal_name,
    	  CASE  WHEN ca.animal_id IS NULL THEN 0 
                    ELSE 1 
              END AS IsSelected	 
       from   lookup_animal la 
    	    LEFT JOIN customer_animals ca ON la.animal_id = ca.animal_id
       order by animal_name asc
    <cfoutput query="get_animals">
        <input name="animal_id" 
    		value="#animal_id#" <cfif IsSelected>checked</cfif/> #animal_name#<br />

    Open in new window

    LVL 2

    Author Closing Comment

    Very good!  Thank you for your quick response - works perfect.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Suggested Solutions

    Title # Comments Views Activity
    Cold Fusion Session Timing Out 11 45
    IT Company 5 52
    Upload PDF On Mobile 1 35
    Video will not go into background 20 16
    I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authoriā€¦
    I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
    This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmakerā€¦
    The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now