Copy link to clipboard
Copied
I'm creating a cascading select drop dwon with ColdFusion ajax from Ben Forta:
http://forta.com/blog/index.cfm/2007/5/31/ColdFusion-Ajax-Tutorial-2-Related-Selects
But it is not entirely working. The first drop down works and shows the countries in a drop down but the second drop down doesn't show the related States!
I did some googling and found out that this is a problem of CF8 (?) because I'm using Application.cfc
To get around this, I found Ray Camden's blog that said the following:
There is a way around it. Sean Corfield has a nice little work around that involves adding the following code to your onRequestStart (not onRequest, but onRequestStart):
1<cfif listlast(arguments.thePage,".") is "cfc">
2<cfset StructDelete(this, "onRequest") />
3<cfset StructDelete(variables,"onRequest")/>
4</cfif>
I already put this code on my OnRequest function in my Application.cfc but I don't quite understand what is .thePage actually mean??
I replaced argument.thePage with argument.Name_of_My_cfc which is AjaxCountryProvince:
But simply added this code like this (see below), it is still not working. Please help!!
<cffunction name="onRequestStart" returnType="string" output="false">
<cfif ListLast(arguments.AjaxCountryProvince,".") is "cfc">
<cfset StructDelete(this, "onRequest") />
<cfset StructDelete(variables,"onRequest")/>
</cfif>
<cfset request.mySecretKey = application.mySecretKey />
<cfset request.algorithm = "AES" />
<cfset request.encoding = "hex" />
</cffunction>
Please advice, I've been stucked with this functionality for two weeks
Copy link to clipboard
Copied
There is a little piece of information missing from Ray's blog post. The onRequestStart() method requires an argument through which ColdFusion can pass the name of the template being called. So you need to add this line right below the <cffunction> tag:
<cfargument name="thePage" type="string" required="true">
Then change the <cfif> tag to what was in the blog post:
<cfif listlast(arguments.thePage,".") is "cfc">
So now what the onRequestStart() code is doing is examining the name of the template being called, and if it ends in "cfc", deletes the onRequest() method from the instance of application.cfc being used to process this AJAX request.
HTH,
-Carl V.
Copy link to clipboard
Copied
Thank you, I tried your suggestion (see below).
I hope I did it right. But my dropdown is still producing the same error (see below)
I thought this work around will fix it but unfortunately not.
<cffunction name="onRequestStart" returnType="string" output="false">
<cfargument name="thePage" type="string" required="true">
<cfif listlast(arguments.thePage,".") is "cfc">
<cfset StructDelete(this, "onRequest") />
<cfset StructDelete(variables,"onRequest")/>
</cfif>
<cfset request.mySecretKey = application.mySecretKey />
<cfset request.algorithm = "AES" />
<cfset request.encoding = "hex" />
</cffunction>
Here is my component:
<CFCOMPONENT output="false">
<!--- Get array of media types --->
<cffunction name="getCountry" access="remote" returnType="array">
<!--- Define variables --->
<cfset var data="">
<cfset var result=ArrayNew(2)>
<cfset var i=0>
<!--- Get data --->
<cfquery name="data" datasource="#application.dsn#">
SELECT CountryId, Country
FROM Country
ORDER BY Country
</cfquery>
<!--- Convert results to array --->
<cfloop index="i" from="1" to="#data.RecordCount#">
<cfset result[1]=data.CountryId>
<cfset result[2]=data.Country>
</cfloop>
<!--- And return it --->
<cfreturn result>
</cffunction>
<!--- Get Region --->
<cffunction name="getProvince" access="remote" returnType="array">
<cfargument name="CountryId" type="numeric" required="true">
<!--- Define variables --->
<cfset var data="">
<cfset var result=ArrayNew(2)>
<cfset var i=0>
<!--- Get data --->
<cfquery name="data" datasource="#application.dsn#">
SELECT RegionId, Region
FROM States
WHERE CountryId = <cfqueryparam cfsqltype="cf_sql_numeric" value="#Trim(arguments.CountryId)#">
ORDER BY Region
</cfquery>
<!--- Convert results to array --->
<cfloop index="i" from="1" to="#data.RecordCount#">
<cfset result[1]=data.RegionId>
<cfset result[2]=data.Region>
</cfloop>
<!--- And return it --->
<cfreturn result>
</cffunction>
</CFCOMPONENT>
Here is the drwop down select:
Select Country:
<cfselect name="CountryId" bind="cfc:cfcomponents.AjaxCountryProvince.getCountry()" bindonload="true" class="SelectReq"/>
Select Province/State:
<cfselect name="RegionId" bind="cfc:cfcomponents.AjaxCountryProvince.getProvince({CountryId})" required="TRUE" class="SelectReq"/>
What have I done wrong?????
Copy link to clipboard
Copied
My guess is that since you the returnType on your function is set to "array", it is trying to send a native ColdFusion array back to the JavaScript code generated by the <CFSELECT> tag. I have'nt used <CFSELECT>, or the binding to CFC that is built into it, so I can't say with authority that this is definitely your problem. You might use the debugging tools in Chrome or Firefox to examine the AJAX data being returned by your CFCs and see if they are in the right format.
-Carl V.