Solved

need both name and ID of country in form

Posted on 2011-02-21
6
388 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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.

Join & Write a Comment

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…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

759 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

24 Experts available now in Live!

Get 1:1 Help Now