Solved

Preselected Dropdown List w/Multiple Selections enabled

Posted on 2013-11-19
3
290 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

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…
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

728 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