Solved

need both name and ID of country in form

Posted on 2011-02-21
6
399 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
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 …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 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