Solved

How do I: Cfform- cfselect- bind - Show cities in a drop down when state is selected

Posted on 2009-04-02
4
914 Views
Last Modified: 2013-12-24
I am attempting to bind a cfselect to show the cities that are in the state they select on a cfform.

form:



<cfselect name="state" size="1" id="state"class="style3">

                                                                          <option value="Select" selected>Select State</option>

                                                                          <option value="AK">AK</option>

                                                                          <option value="AL">AL</option>

                                                                          <option value="AR">AR</option>

                                                                          <option value="AZ">AZ</option>

                                                                          <option value="CA">CA</option>

                                                                          <option value="CO">CO</option>

                                                                          <option value="CT">CT</option>

                                                                          <option value="DC">DC</option>

                                                                          <option value="DE">DE</option>

                                                                          <option value="FL">FL</option>

                                                                          <option value="GA">GA</option>

                                                                          <option value="HI">HI</option>

                                                                          <option value="IA">IA</option>

                                                                          <option value="ID">ID</option>

                                                                          <option value="IL">IL</option>

                                                                          <option value="IN">IN</option>

                                                                          <option value="KS">KS</option>

                                                                          <option value="KY">KY</option>

                                                                          <option value="LA">LA</option>

                                                                          <option value="MA">MA</option>

                                                                          <option value="MD">MD</option>

                                                                          <option value="ME">ME</option>

                                                                          <option value="MI">MI</option>

                                                                          <option value="MN">MN</option>

                                                                          <option value="MO">MO</option>

                                                                          <option value="MS">MS</option>

                                                                          <option value="MT">MT</option>

                                                                          <option value="NC">NC</option>

                                                                          <option value="ND">ND</option>

                                                                          <option value="NE">NE</option>

                                                                          <option value="NH">NH</option>

                                                                          <option value="NJ">NJ</option>

                                                                          <option value="NM">NM</option>

                                                                          <option value="NV">NV</option>

                                                                          <option value="NY">NY</option>

                                                                          <option value="OH">OH</option>

                                                                          <option value="OK">OK</option>

                                                                          <option value="OR">OR</option>

                                                                          <option value="PA">PA</option>

                                                                          <option value="RI">RI</option>

                                                                          <option value="SC">SC</option>

                                                                          <option value="SD">SD</option>

                                                                          <option value="TN">TN</option>

                                                                          <option value="TX">TX</option>

                                                                          <option value="UT">UT</option>

                                                                          <option value="VT">VT</option>

                                                                          <option value="VA">VA</option>

                                                                          <option value="WA">WA</option>

                                                                          <option value="WI">WI</option>

                                                                          <option value="WV">WV</option>

                                                                          <option value="WY">WY</option>

                                                                          <option value="ZZ">ZZ</option>

                                                                          </cfselect>
 

<cfselect name="city"

            bind="cfc:citylookup.cityfind({state})" />

 (I have no idea how to show values in drop down with cfc)
 
 
 

CFC: citylookup.cfc
 

<cfcomponent output="false">
 

<cfset THIS.dsn="mydsn">
 

   <cffunction name="cityfind" access="remote" returnType="array">

      <cfargument name="state" type="string" required="true">
 

      <!--- Define variables --->

      <cfset var data="">

      <cfset var result=ArrayNew(1)>

      <cfset var i=0>
 

      <!--- Get data --->

      <cfquery datasource="#THIS.dsn#" name="data">

      SELECT city

      FROM Zipcodeworld

      WHERE state = '#ARGUMENTS.state#'

      ORDER BY City

      </cfquery>

   

      <!--- Convert results to array --->

      <cfloop index="i" from="1" to="#data.RecordCount#">

      

         <cfset result[i][1]=Data.city[i]>
 

      </cfloop>
 

      <!--- And return it --->

      <cfreturn result>

   </cffunction>

Open in new window

0
Comment
Question by:LeadCo
  • 2
  • 2
4 Comments
 
LVL 27

Accepted Solution

by:
azadisaryev earned 500 total points
ID: 24057001
first, you no longer need to convert the query to an array an return that array from your cfc function - CF8 supports plain old query to populate your cfselect.
so you can change your cfc function to a simple:

<cffunction name="cityfind" access="remote" returnType="QUERY">
  <cfargument name="state" type="string" required="true">
  <!--- Define variables --->
  <cfset var data="">
  <!--- Get data --->
  <cfquery datasource="#THIS.dsn#" name="data">
  SELECT city
  FROM Zipcodeworld
  WHERE state = <cfqueryparam cfsqltype="cf_sql_varchar" value="#ARGUMENTS.state#">
  ORDER BY City
  </cfquery>
  <cfreturn data />
</cffunction>


> I have no idea how to show values in drop down with cfc

by using DISPLAY and VALUE attributes and specifying in them a query column (city for both in your case, since your query returns one column only):

<cfselect name="city" bind="cfc:citylookup.cityfind({state})" DISPLAY="city" VALUE="city" />

Azadi
0
 

Author Comment

by:LeadCo
ID: 24057129
HMMMMM. I used that exact code and it did not place values in city cfselect after selecting a state.

I tried.

<cfselect name="city" bind="cfc:citylookup.cityfind({state})" DISPLAY="city" VALUE="city" />

<cfselect name="city" bind="cfc:citylookup.cityfind({state})" DISPLAY="data.city" VALUE="data.city" />

<cfselect name="city" bind="cfc:citylookup.cityfind({state})" DISPLAY="result.city" VALUE="result.city" />


No Luck.....
0
 
LVL 27

Assisted Solution

by:azadisaryev
azadisaryev earned 500 total points
ID: 24057669
oh, right, cfselect must have a closing </cfselect> tag for this to work - it does not work with <cfselect .. /> syntax:

<cfselect name="city" bind="cfc:citylookup.cityfind({state})" DISPLAY="city" VALUE="city"></cfselect>

Azadi
0
 

Author Closing Comment

by:LeadCo
ID: 31566124
Thanks!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Column Spacing 3 58
Web server settings related to keepalive 1 110
ColdFusion 10 Error 2 37
Automate ColdFusion Internet Form with VBA from Access 10 37
This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
The purpose of this video is to demonstrate how to automatically show related posts at the bottom of a blog post in WordPress. This will be demonstrated using a Windows 8 PC. Plugin “Yet Another Related Posts Plugin” will be used. Go to your…
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now