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
Solved

Preselected Dropdown List w/Multiple Selections enabled

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Can I have multiple sites sharing the same codebase? 10 66
Mixing cold fusion headers and footers with PHP body 6 65
C# script to Coldfusion 25 84
Database set up 5 79
This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

839 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