Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Preselected Dropdown List w/Multiple Selections enabled

Posted on 2013-11-19
3
Medium Priority
?
298 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 2000 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

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…
CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

604 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