Skip to main content
Inspiring
Dec 21, 2022 11:19 PM UTC
Question

cfselect works in one form but not in the other, Please HELP!

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!

This topic is closed to new replies. Start a new post to keep the conversation going.

5 replies

BKBK
Community Expert
Community Expert
Dec 22, 2022 6:20 PM UTC

Perhaps ColdFusion is confused by the values of cfselect's "selected" attributes. That is,

 selected="#GetUser.State#" 
 selected="#GetUser.Zip#"
 selected="#GetUser.City#"

I think you should delete them. You could then streamline your CFC as follows:

<!--- Create the default selection for country--->
<cfset CountryResult[1][1]="">
<cfset CountryResult[1][2]="Select Country">

<!--- Convert query results to array --->
<cfloop query="q_Country">
	<cfset CountryResult[currentrow+1][1]=Country_id>
	<cfset CountryResult[currentrow+1][2]=Country>
</cfloop>

 

<!--- Create the default selection for city--->
<cfset CityResult[1][1]="">
<cfset CityResult[1][2]="Select City">

<!--- Convert query results to array --->
<!--- Note that the ID comes first! --->
<cfloop query="q_Cities">
	<cfset CityResult[currentrow+1][1]=city_id>
	<cfset CityResult[currentrow+1][2]=city>
</cfloop>

 

Translate
mega_LAuthor
Inspiring
Dec 31, 2022 4:38 PM UTC

Hi!Thank you for helping. I tried your suggestion with the functions but I still get the same error message. I modified my functions per your suggestion and tested them. Both are working and returning values (see partial screen shot on countries and cities). Actually I also tested my old functions. They were also working. The error shows up as soon as  the form is loaded. I got rid of all the selected attributes down to this:

<cfselect name="DepCountry" bind="cfc:cfcomponents.RequestLoc.GetCountries()" bindonload="true" required="yes"/>

<cfselect name="DepCity" bind="cfc:cfcomponents.RequestLoc.GetCities({country_id})"/>

The error message persist. The error still says:

Bind failed, element not found:country_id [enabled debugging by adding 'cfdebug' to your url parameters to see more information] - OK button

 

The first drop down is populated but I can't click the arrow. It freezes. The second drop down is not populated. Also I'm trying to follow what the error message says about putting cfdebug in the url may be I can see more error message but when I put ?cfdebug in the url the same message pop up again.

I'm thinking the problem somehow point to the country_id on the second cfselect. This id is somehow does not get detected by ColdFusion, is it possoble?

Translate
BKBK
Community Expert
Community Expert
Dec 31, 2022 5:11 PM UTC

Perhaps ColdFusion expects the defaults to have a value other than the empty string. So, experiment by replacing

<cfset CountryResult[1][1]="">

 and

<cfset CityResult[1][1]="">

respectively with

<cfset CountryResult[1][1]="0">

 and

<cfset CityResult[1][1]="0">

 

Translate