Solved

Drop-down box displays current choice twice when editing form

Posted on 2012-03-27
4
434 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
[X]
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
  • 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

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…
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…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

729 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