Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Drop-down box displays current choice twice when editing form

Posted on 2012-03-27
4
Medium Priority
?
441 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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

610 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