Solved

Preselected Dropdown List w/Multiple Selections enabled

Posted on 2013-11-19
3
289 Views
Last Modified: 2013-11-19
I have a dropdown list which allows multiple selections. A user can choose as many of the items they want. Upon submission, it writes those choices in a single form field as a comma separated list. The dropdown list field is called KEYWORDS.

So, my question is how do I take what they chose originally from the database and repopulate the KEYWORDS dropdown list with the choices they made originally.

As it stands, the values in the database are

option8,option14,option28,option41

So when I go to reload the page form with only those options chosen from that KEYWORDS dropdown field, I want those highlighted.

I write this in CF and think I need to use a CFLOOP "List" but don't know how to do that when the KEYWORDS dropdown is being populated from a query result already.

<cfquery name="keywords">
SELECT keyword FROM keywords
WHERE 1=1
</cfquery>

<select name="keywords" id="keywords">
<cfloop query="keywords">
<option value="#keyword#">#keyword#</option>
</cfloop>
</select>

Open in new window


I know that I can put a CFIF statement in there to "select" it, but I don't know how to write the cfif statement and where i can compare it to the database results from before which is "option8,option14,option28,option41"

I think I need to compare option 8 first then option14 then option 28, etc. I just don't know how to write the conditional cfloop and how to nest it in the code.

HELP!
0
Comment
Question by:day6
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 39660341
You should be able to use list functions, in place of a loop. If the current keyword is in the "chosen" list, then select it:

        ...
      <cfloop query="keywords">
            <option value="#keyword#" <cfif listFindNoCase(chosenOptions, keyword)>selected</cfif>>#keyword#</option>
      </cfloop>
        ....

Here's a complete runnable example to illustrate:

<!--- just for debugging to simulate your db query --->
<cfset keywords = queryNew("keyword")>
<cfloop from="1" to="41" index="x">
	<cfset row = queryAddRow(keywords, 1)>
	<cfset keywords.keyword[ row ] = "option"& x>
</cfloop>

<!--- simulate "chosen" options stored in db --->
<cfset selectedOptions = "option1,option8,option14,option28,option41">
	
<cfoutput>
<form>
	<select name="keywords" id="keywords" multiple="true" size="8">
	<cfloop query="keywords">
		<option value="#keyword#" <cfif listFindNoCase(selectedOptions, keyword)>selected</cfif>>#keyword#</option>
	</cfloop>
	</select>
</cfoutput>
</form>

Open in new window

0
 
LVL 1

Author Closing Comment

by:day6
ID: 39660380
I feel dumb, but this saved me like hours of experimenting.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 39660391
Yeah, I spent way too long on it the 1st time I needed to do that.  At least it finally paid off for someone ;-)
0

Featured Post

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Coldfusion Report Builder HTML table 2 83
segregating a list into two lists 4 84
Coldfusion grab data from multi level structures 3 85
ColdFusion 10 Error 2 84
Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 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