coldfusion cfform autosuggest - how to pass the locationid when the user is being suggested the locationname?

125 Views Asked by At

I've decided it's about time I started using cfform (3 mins in and I've already hit my first stumbling block!)

I want to have an autosuggest text input for users to choose a city. In my db I have locationnames and locationids.

e.g.

----------------------------
locationid   |   locationname
----------------------------
1      |      London
2      |      New York
3      |      Paris
etc.
-----------------------------

The user will start to type the locationame - I want autosuggest to show locationanames that match, but when they submit the form I actually want the locationid to be passed.

How on Earth do I do this with cfform?

2

There are 2 best solutions below

2
On

Don't ever use cfform or any other UI-related Coldfusion crap in the first place.
Take a look at the excellent Typeahead JQuery plugin from Twitter instead: http://twitter.github.io/typeahead.js/examples/

1
On

You can actually just use the location name and look up the id upon submission, as long as your location names are unique. If they're not, as was the example about London, Ontario, then you need to include the state or county name in the lookup value, separated by commas so you can parse the location name back out again on form submission. A regular expression can take out the locationName in one group and the stateName in the next group. Then do a query where location = locationName and state = stateName.