Solved

Drop-down box displays current choice twice when editing form

Posted on 2012-03-27
4
414 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_
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks; once again.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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 …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now