Solved

Drop-down box displays current choice twice when editing form

Posted on 2012-03-27
4
428 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
COLD FUSION 6 27
Problem in SELECT Statement 5 88
CSS HELP 6 90
how to open dropdown menu on page load without having to click? 4 95
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…
This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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