Search code examples
cfmlcoldfusion-11

cfselect has duplicate options when pulling a record into a form


I am trying to get the form on my edit screen to pull what is in the record selected, and add the options that are still available that dont include what is selected. So if the current record has a ROC_Group of 3, the dropdown has 3 selected, and 4 and 5 as options. This is as close as I can get it, but end up with what is currently in the record (3), and 3,4 and 5 as options. So it looks like there is a duplicate in the dropdown. Any suggestions? Thank you

<cfselect name="ROC_GROUP"  ><cfoutput query="GetSiteNotoUpdate">   
            <cfif GetSiteNotoUpdate.ROC_GROUP is "#ROC_GROUP#">
                <option value="#ROC_GROUP#" selected="yes">#ROC_GROUP#</option>
                <option>3</option><option>4</option><option>5</option>
        <cfelse>
                <option value="#ROC_GROUP#">#ROC_GROUP#</option>    
        </cfif>     
        </cfoutput></cfselect>

Solution

  • <cfif GetSiteNotoUpdate.ROC_GROUP is "#ROC_GROUP#">

    Your comparison is using two variables, both named ROC_GROUP. If the second one refers to a different variable than GetSiteNotoUpdate.ROC_GROUP, you need to scope it. Otherwise, CF will not know which one you mean and will likely interpret the above as <cfif someQueryVariable equals itself>, which is always true.

    Simply put the "currently selected" value into a separate variable, like CurrentlySelectedGroup. Then compare it with each value in the query inside your loop:

    <select name="ROC_GROUP">
       <cfoutput query="GetSiteNotoUpdate"> 
           <option value="#ROC_GROUP#" 
                <cfif GetSiteNotoUpdate.ROC_GROUP eq CurrentlySelectedGroup>selected="yes"</cfif>>  
                #ROC_GROUP#
           </option>
        </cfoutput>
    </select>
    

    As an aside, since you are not using any of the extra features, there is no need to use <cfselect>. Just use a plain html <select>.