cfselect works in one form but not in the other, Please HELP!
- December 21, 2022
- 1 reply
- 809 views
Just learned about CF2021, downloaded the 2021 developer version and having weird issues with cfselect tag.
I have 2 forms, registration and request forms. both use cfselect tags to create two or three dependent dropdowns.
On my registration form:
I have 3 dependent dropdowns. A states dropdown, zip codes and US cities dropdown all use cfselect. When user select a state, related zipcodes populate the second dropdown and then when a zipcode is selected the related city gets selected in the 3rd dropdown. All work great. The selected, value and message attributes all are working just great! (codes below):
States dropdown:
<cfselect name="StateCode" bind="cfc:cfcomponents.RegLocation.GetStates()" selected="#GetUser.State#" bindonload="true" required="yes" message="Please select your State" class="smallstylebox" value="#Trim(GetUser.State)#"/>
ZipCode dropdown:
<cfselect name="ZipCode" bind="cfc:cfcomponents.RegLocation.GetZipCode({StateCode})" selected="#GetUser.Zip#" class="smallstylebox"/>
City dropdown:
<cfselect name="City" bind="cfc:cfcomponents.RegLocation.GetCities({ZipCode})" selected="#GetUser.City#" class="stylebox"/>
Unfortunately, The 2 dependent cfselect in my Request form for some reason DO NOT WORK!. I've been debugging for a few days and CANNOT find anything different that what I've done at the above registration form and get very frustrated and thinking of abandon using coldfusion. It doesn't seem I can find any help googling. Not may people seem to use ColdFusion anymore.
Here is the 2 cfselect that have given me problem. The first cfselect works but the attributes (selected, value and message are ignored). I just got a populated dropdown and that's it. The function on the cfc work, been tested and return the arrays.
Country dropdown:
<cfquery name="GetUserCountry" datasource="#Trim(application.dsn)#">
SELECT Country,State,City FROM users
WHERE UserId = <cfqueryparam cfsqltype="cf_sql_integer" value="#Trim(session.UserId)#">
</cfquery>
<cfselect name="DepCountry" bind="cfc:cfcomponents.RequestLoc.GetCountries()" selected="#Trim(GetUserCountry.Country)#" bindonload="true" required="yes" message="Please select your State" class="smallstylebox" value="#Trim(GetUserCountry.Country)#"/>
the Cities dropdown:
This doesn't work and gives out error:
Bind failed, element not found: country_id [Enable debugging by adding 'cfdebug' to your URL parameters to see more information]
<cfselect name="DepCity" bind="cfc:cfcomponents.RequestLoc.GetCities({country_id})" selected="#Trim(GetUserCountry.City)#" class="smallstylebox"/>
When I put cfdebug on my url:
http://127.0.0.1:8500/MyTest/postlisting.cfm?cfdebug
the same error message show up. So I can't see what's going on and why country_id is not passed. It was working find on the Registration form, right?
Here is the cfc:
<cfcomponent displayname="FormListing" hint="Getting data for Countries and Cities">
<CFFUNCTION name="Init" access="public" returntype="any" output="false" hint="Returns an initialized component instance.">
<!--- Return This reference. --->
<cfreturn THIS />
</CFFUNCTION>
<!--- ********** COUNTRY ************ --->
<CFFUNCTION name="GetCountries" access="remote" returnType="array" output="false" hint="Getting Departure Country">
<!--- Define variables --->
<cfset CountryResult=ArrayNew(2)>
<cfset i=0>
<cfquery name="q_Country" datasource="#application.dsn#">
SELECT DISTINCT country_id,country
FROM WorldCities
ORDER By country ASC
</cfquery>
<!--- Convert results to array --->
<cfloop index="i" from="1" to="#q_Country.RecordCount#">
<cfset CountryResult[i][1]=q_Country.country_id[i]>
<cfset CountryResult[i][2]=q_Country.country[i]>
</cfloop>
<CFRETURN CountryResult>
</CFFUNCTION>
<!--- ********** Cities ************ --->
<CFFUNCTION name="GetCities" access="remote" returnType="array" output="false" hint="Get Related Cities">
<cfargument name="country_id" type="integer" required="true">
<!--- Define variables --->
<cfset CityResult=ArrayNew(2)>
<cfset i=0>
<cfquery name="q_Cities" datasource="#application.dsn#">
SELECT city,citi_id
FROM WorldCities
WHERE country_id = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(arguments.country_id)#">
ORDER BY city ASC
</cfquery>
<!--- Convert results to array --->
<cfloop index="i" from="1" to="#q_Cities.RecordCount#">
<cfset CityResult[i][1]=q_Cities.city[i]>
<cfset CityResult[i][2]=q_Cities.city_id[i]>
</cfloop>
<CFRETURN CityResult>
</CFFUNCTION>
</cfcomponent>
Can anyone help me please? Thank you!
