Solved

Preselected Dropdown List w/Multiple Selections enabled

Posted on 2013-11-19
3
282 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
  • 2
3 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
Comment Utility
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
Comment Utility
I feel dumb, but this saved me like hours of experimenting.
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now