Go Premium for a chance to win a PS4. Enter to Win

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 352
  • Last Modified:

Auto Select Cfselect tag

I currently have a cfselect tag that is populated via a query. I'm trying to get the queried results to automatically select an entry based upon a condition (url.contactid).
QUESTION: How do I get the cfselect tag to automatically select an entry base upon a variable either URL or FORM?

<cfselect name="ContactID" size="25" id="ContactID"> <cfoutput query="contacts">
          <option value="#contacts.ContactID#">#contacts.LastName#, #contacts.FirstName# - #contacts.EmailAddress# ---  #contacts.Company#</option>
        </cfoutput> </cfselect>

Open in new window

  • 2
1 Solution
like this:

<cfselect name="ContactID" size="25" id="ContactID">
<cfoutput query="contacts">
  <option value="#contacts.ContactID#" <cfif contacts.contactID EQ SomeValriable>selected="selected"</cfif>>#contacts.LastName#, #contacts.FirstName# - #contacts.EmailAddress# ---  #contacts.Company#</option>

or, if you edited your query and added a new column by concatenating all the column you use in your select's display values, then you could just use 1 line of code to create your cfselect:

<cfselect name="contactID" size="25" QUERY="contacts" DISPLAY="concatenated-qiery-column-name" VALUE="contactID" SELECTED="#SomeVariable#"></cfselect>

jasch2244Author Commented:
Does the "selected" portion of the code refer to a variable as well? example: selected="selected"
no, selected="selected" is just a standard XHTML way to mark a selected option. it is not any cf variable.
you can just use 'selected' if your page's DOCTYPE is not XHTML or if you do not care about your code being XHTML-compliant:

<option value="#contacts.ContactID#" <cfif contacts.contactID EQ SomeValriable>selected</cfif>>...</option>


Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now