Solved

need both name and ID of country in form

Posted on 2011-02-21
6
413 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Building an interactive eFuture classroom

Watch and learn how ATEN provided a total control system solution including seamless switching matrix switch, HDBaseT extenders, PDU, lighting control to build an interactive eFuture classroom.

 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
Suggested Courses

737 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