Solved

Drop-down box displays current choice twice when editing form

Posted on 2012-03-27
4
424 Views
Last Modified: 2012-03-27
I am trying to build a drop-down choice that does not query a database - basically creating the choices with html. I want to write the selected value to my database.

When someone visits the page and need to make a change to the value it shows up in the drop down twice - the current value and then the choices again. For instance, if the choices are cat, dog and rabbit... if cat was previously selected it will show cat twice.

Below is my drop-down. Can someone help me display the current database choice and then only show the "other" choices?

<CFSELECT NAME="PetType">

<cfoutput><OPTION>#PetType#</OPTION></cfoutput>
      <OPTION VALUE="Cat">Cat</OPTION>
      <OPTION VALUE="Dog">Dog</OPTION>
      <OPTION VALUE="Rabbit">Rabbit</OPTION>
</CFSELECT>
0
Comment
Question by:earwig75
  • 2
  • 2
4 Comments
 
LVL 52

Expert Comment

by:_agx_
ID: 37771945
Right, that's is exactly what should happen with the code above.

To avoid that you need to compare each value against the previous #PetType#. If they match mark the <option> as "selected" - instead of creating a new list option.  (I remember you did something similar on another screen)

If your list is really hard coded:
        <select ...>
            <OPTION VALUE="Cat" <cfif PetType eq "Cat">selected</cfif>>Cat</OPTION>
            <OPTION VALUE="Dog" <cfif PetType eq "Dog">selected</cfif>>Dog</OPTION>
            .. remaining options
        </select>

... or if the number of options is small, you could streamline it by putting the values into a list. Then loop through it. But you still have to compare the individual "VALUES"

        <cfset yourList="cat,dog,etc...">
        <select ...>
            <cfoutput>
            <cfloop list="#yourList#" index="currPet">
                <OPTION VALUE="#currPet#" <cfif PetType eq currPet>selected</cfif>>#currPet#</OPTION>
             </cfloop>
            </cfoutput>
        </select>

EDIT: Just noticed you said you're NOT using a query. But I'll leave the code below for posterity

.. but assuming you're populating the list from a query:

        <select ...>
         <cfoutput query="yourQuery">
             <OPTION VALUE="#TheValueColumn#" <cfif PetType eq TheValueColumn>selected</cfif>>#TheDisplayColumn#</OPTION>
         </cfoutput>
        </select>
0
 

Author Comment

by:earwig75
ID: 37772086
Thanks for the reply but I am still confused. I tried the below but it is still repeating the choice that was previously chosen. This form field needs to work with new record entries as well.

<CFSELECT NAME="PetType">

<cfoutput><OPTION Value=#PetType#<cfif PetType eq variables.PetType>selected</cfif>>#PetType#</OPTION>
</cfoutput>
      <OPTION VALUE="Cat">Cat</OPTION>
      <OPTION VALUE="Dog">Dog</OPTION>
      <OPTION VALUE="Rabbit">Rabbit</OPTION>
</CFSELECT>
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 37772147
Not sure how you got that from what I posted ;-) It's totally different. Take another look at examples #1 and #2 above.  Barring any typos they should work correctly "as is".

Here's example # 1 again. I just changed the select to cfselect. Notice with a manually built select list you have to compare each value individually.
 
<CFSELECT NAME="PetType">
            <OPTION VALUE="Cat" <cfif PetType eq "Cat">selected</cfif>>Cat</OPTION>
            <OPTION VALUE="Dog" <cfif PetType eq "Dog">selected</cfif>>Dog</OPTION>
            <OPTION VALUE="Rabbit" <cfif PetType eq "Rabbit">selected</cfif>>Rabbit</OPTION>
</cfselect>
0
 

Author Closing Comment

by:earwig75
ID: 37772210
Thanks; once again.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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 …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

832 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