Skip to main content
Inspiring
November 7, 2022
Answered

Problem showing selected item with cfselect binding

  • November 7, 2022
  • 1 reply
  • 265 views

How can I keep the selected item with cfselect binding? When user selected a State (Delaware) from the dropdown Alabama shows up as the first State once the form refreshed instead of Delaware. CFSELECT has a selected attribute but I don't know what value should go there and not sure if this is the one I have to use to keep the selected value. I've been searching with no luck so far, Can anyone help? 

 

another question is how to cfdump cfselect binding to see what have is return?

 

DependentSelect.CFC:
<CFFUNCTION name="GetStates" access="remote" returnType="array" hint="Get US 50 States">
    <!--- Define variables --->
    <cfset var StateResult=ArrayNew(2)>
    <cfset var i=0>

 

    <cfquery name="q_States" datasource="#Trim(application.dsn)#">
     SELECT DISTINCT StateName,StateCode
     FROM States
     ORDER By StateName
    </cfquery>


   <!--- Convert results to array --->
   <cfloop index="i" from="1" to="#q_States.RecordCount#">
       <cfset StateResult[i][1]=q_States.StateCode[i]>
       <cfset StateResult[i][2]=q_States.StateName[i]>
  </cfloop>

 

  <CFRETURN StateResult>

</CFFUNCTION>

 

My ProfileForm.cfm:

<cfform id="profile" action="profileaction.cfm" method="post" enctype="multipart/form-data">

.........other form fields here.....

 

<label class="labels">Select your State</label>
<cfselect name="StateCode" bind="cfc:cfcomponents.DependentSelect.GetStates()" selected="??" bindonload="true" required="yes" message="Please select your State" class="smallstylebox" value="#Trim(GetUser.State)#"/>

</cfform>

 

I am using coldfusion 2021 on my local, SQL Server Management Studio V 18.12.1

This topic has been closed for replies.
Correct answer BYJ_wntrsnt

Hi Charlie, thank you so much for the answer to cfselect. I got it working nicely. As for the cfdump I remember on the older version of CF when I used binding like what iuse here with cfselect case here (<cfselect name="StateCode" bind="cfc:cfcomponents.DependentSelect.GetStates()" selected="#GetUser.State#" bindonload="true" required="yes value="#Trim(GetUser.State)#"/>)  I kind of remember I have to put something in the url to get a small window pop up with some debug codes on it. But I can't exactly remember how to do it now since I have not worked with CF for awhile. What I did in the past when I used binding and wanted to know why I got the error I think I did something to get that window opo up to see the debug codes while I was still in the form and hit submit(?)

1 reply

Charlie Arehart
Community Expert
Community Expert
November 8, 2022

Selected would be set to a string (or a variable), such that would match one of  your getuser.state values being put in the "value" attribute.

 

As for a cfdump, you could dump the form scope, or form.statecode specifically--at lest after they hit submit (if you add one) or press enter while on a text field. 

/Charlie (troubleshooter, carehart. org)
BYJ_wntrsntAuthorCorrect answer
Inspiring
November 8, 2022

Hi Charlie, thank you so much for the answer to cfselect. I got it working nicely. As for the cfdump I remember on the older version of CF when I used binding like what iuse here with cfselect case here (<cfselect name="StateCode" bind="cfc:cfcomponents.DependentSelect.GetStates()" selected="#GetUser.State#" bindonload="true" required="yes value="#Trim(GetUser.State)#"/>)  I kind of remember I have to put something in the url to get a small window pop up with some debug codes on it. But I can't exactly remember how to do it now since I have not worked with CF for awhile. What I did in the past when I used binding and wanted to know why I got the error I think I did something to get that window opo up to see the debug codes while I was still in the form and hit submit(?)

Charlie Arehart
Community Expert
Community Expert
December 12, 2022

Sorry I missed your follow-up question. I think what you're thinking of (the ?cfdebug feature) is discussed in the docs here.

 

BTW, if that helps you, then if this answered the second part and my first one answered the first part, I hope you will mark both of them as answers. It's ok to have more than one reply marked as an answer. (And we "Adobe community professionals" do get assessed based on both how many replies we offer and how many are marked as answers.)

/Charlie (troubleshooter, carehart. org)