3 Replies Latest reply on Sep 16, 2006 4:25 PM by azadisaryev

    cfselect

    Level 7
      I am using cfselect to populate a drop down list on a form. Is there a way
      to set the initially selected value from a database result?

      I.E. If the 1st query pulls and populates the list with: Apples, Oranges,
      Pears
      The 2nd query has the value of Oranges
      Then the form will show the Oranges initially selected.

      I have this so far:

      <cfselect name="client" size="1" query="clients" value="username"
      display="company" required="yes" message="Please choose a
      client"></cfselect>

      --
      Wally Kolcz
      Developer / Support


        • 1. Re: cfselect
          Dan Bracuk Level 5
          selected="#query2.fieldname[rownumber]#"
          • 2. Re: cfselect
            Level 7
            If you want Oranges selected do this.

            <cfselect name="client" size="1" required="yes" message="Please choose a
            client" query="clients" value="username"
            display="company" selected="Oranges"></cfselect>


            "Wally Kolcz" <wkolcz@projectproofing.com> wrote in message
            news:ebtp6s$806$1@forums.macromedia.com...
            >I am using cfselect to populate a drop down list on a form. Is there a way
            >to set the initially selected value from a database result?
            >
            > I.E. If the 1st query pulls and populates the list with: Apples, Oranges,
            > Pears
            > The 2nd query has the value of Oranges
            > Then the form will show the Oranges initially selected.
            >
            > I have this so far:
            >
            > <cfselect name="client" size="1" query="clients" value="username"
            > display="company" required="yes" message="Please choose a
            > client"></cfselect>
            >
            > --
            > Wally Kolcz
            > Developer / Support
            >


            • 3. Re: cfselect
              azadisaryev Level 1
              Rick has got it totally right. One things to keep in mind about <cfselect>:
              - 'selected' attribute references the VALUE of the cfselect option, not displayed text.
              Thus, it must reference one or more valid items from the query column defined in the 'value' attribute of cfselect tag. If your query used in your <cfselect> returns id and title (i.e. 1-apples; 2-oranges), and <cfselect>'s 'value' attribute is set to 'id' query column, then the value of 'selected' attribute must be 1 or 2 (or both, comma-separated, for multiple select lists), and NOT apples or oranges.
              In many, if not most, cases your <cfselect>'s 'value' attribute will be set to the 'id' column of your query, since you are likely passing the selections made in your <cfselect> as parameters to the form's action page or insert them into another table in the database. And in most cases you can not be 100% sure of current 'id' of the item(s) you want pre-selected in your <cfselect>, especially if the table queried to populate your <cfselect> is user-editable (i.e. user can delete the existing 'apples' item from that database table, and then add a new 'apples' item, which will have a new id).
              In such cases it may be better, instead of using <cfselect>, to use a plain html <select> and loop through the query to output <option> tags for it with 'id' as value and 'title' as displayed text. You can then use a <cfif>/<cfelse> conditional construct to assign a selected state to a particular <option> tag based on the tag's displayed text ('title' query column), not its value ('id' query column).
              Example:
              <cfquery name="getFruits" datasource="dsn">
              SELECT id, title FROM fruits ORDER BY id;
              </cfquery>
              let's say the query returns 3 rows (id-title): 1-apples, 2-oranges, 3-peaches.
              you can then use
              <select name="fruit_id" size="1">
              <cfoutput query="getFruits">
              <cfif getFruit.title is "Oranges">
              <option value="#id#" selected>#title#</option>
              <cfelse>
              <option value="#id#">#title#</option>
              </cfif>
              </cfoutput>
              </select>

              The returned value of your selection in this <select> tag will be the selected fruit's id (i.e. #form.fruit_id# will return 2 in the above example based on default selection), but you are pre-selecting an item in the select list based on the fruit's title.