Solved

need both name and ID of country in form

Posted on 2011-02-21
6
393 Views
Last Modified: 2013-12-24
I have a form where the user selects his country. When submitting the form I need the CountrID to be inserted into a table and need the country name to be used in a cfmail.

my combobox looks like this:

<select name="Country" class=formcell id="Country"
                  <cfif isdefined ("form.Country") And #form.Country# IS NOT "" And #form.Country# IS NOT "#variables.FieldLabels['81']#">
                    <cfloop query="getcountry">
                      <option value='#getcountry.mycountry#' <cfif getcountry.mycountry eq form.Country>selected="true"</cfif>>#getcountry.mycountry#</option>
                    </cfloop>
                    <cfelse>
                    <option>#variables.FieldLabels['81']#</option>
                    <cfloop query="getcountry">
                      <option value='#getcountry.mycountry#'>#getcountry.mycountry#</option>
                    </cfloop>
                  </cfif>
                </select>  

from this I get the name of the country when I use: form.Country
how would I get the CountryID from this?   ....my query is below
         
<cfquery name="getcountry" datasource="#application.DS#" username="#application.dbuser#" password="#application.dbpass#">
SELECT     English AS mycountry, Priority, CountryID
FROM         tblCountries
ORDER BY Priority, mycountry
</cfquery>

Open in new window

0
Comment
Question by:Shawn
  • 4
  • 2
6 Comments
 
LVL 52

Expert Comment

by:_agx_
ID: 34947614
So ... #form.Country# stores the name and you want the ID? Just run a query based on name

INSERT INTO OtherTable ( CountryID )
SELECT     CountryID
FROM         tblCountries
WHERE      YourCountryNameField = <cfqueryparam value="#form.country#" cfsqltype="cf_sql_varchar">

0
 
LVL 1

Author Comment

by:Shawn
ID: 34947688
I was thinking I might be able to store the id and the name in the combo box as the actual country name changes depending on the language setting (eg french/english)
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 34947859
It's simpler to just make the ID part of the value

     <option value="#country#|#mycountry#'>#getcountry.mycountry#</option>

Then extract it with list functions

    <cfset countryID = listFirst(form.country, "|")>
    <cfset countryName = listRest(form.country, "|")>
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 52

Expert Comment

by:_agx_
ID: 34947872
<option value="#country#|#mycountry#'>

Typo correction:   <option value="#countryID#|#mycountry#'>

Personally, I'd just store the ID (only). Then lookup the name via query on the action page.  But that's up to you.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 34947942
It's simpler to just make the ID part of the value

Failing that, you could make it an attribute of the <option>.  Then update a hidden field onChange.  Assuming it always fires in your form.

<script type="text/javascript">
	function storeCountryID() {
		var source = document.getElementById('country');
		var opt  = source.options[source.selectedIndex];
		var target = document.getElementById('countryID');
		target.value = opt.getAttribute("countryID");
	}
</script>
....
<select id="country" name="country" onChange="storeCountryID()">
	<option value="">select a country</option>
	<cfoutput query="getcountry">
		<option value="#mycountry#" countryID="#countryID#">#mycountry#</option>
	</cfoutput>	
</select>

<!--- make this field hidden --->
<input type="text" id="countryID" name="countryID" value="" />
....

Open in new window

0
 
LVL 1

Author Closing Comment

by:Shawn
ID: 34966516
nice, thanks agx
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 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

22 Experts available now in Live!

Get 1:1 Help Now