Can this be done? How do I populate a form field (Country) so it shows the viewers country without a country list.
Below is the script that shows the country, how can the result show up as a value in a form field?
Click on the form element, and then choose the Output tab of the
Inspector (icon looks like a newspaper). Note what the Name field has
in it (change this to something meaningful to your server, by the
way). Then, assuming that it is called ‘country’, you could do this:
Add the reference to the geoip.js script to the page using the
Page / HTML Markup dialog in the Before /HEAD section. (That’s just
the first script tag from the code you posted here.)
Add the following script to the page using the Page / HTML Markup
dialog in the Before /BODY section.
<script type="text/javascript">
var country = document.forms[0]['country'].
if (country && geoip_country_name){
country.value = geoip_country_name();
}
</script>
On Sep 10, 2009, at 9:25 AM, Pete wrote:
Can this be done? How do I populate a form field (Country) so it
shows the viewers country without a country list.
Below is the script that shows the country, how can the result show
up as a value in a form field?
Country Name:
document.write(geoip_country_name());
_______________________________________________
freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options
Hi Walter,
Thanks for the instructions. Sorry I can’t get it to work. Country form field is blank? I can’t see how I have mucked it up as your instructions are easy to follow.
Hi Walter,
Thanks for the instructions. Sorry I can’t get it to work. Country
form field is blank? I can’t see how I have mucked it up as your
instructions are easy to follow.
Sorry – typo. I should have left the dot off the end of this line:
var country = document.forms[0][‘country’].
Make it read thus:
var country = document.forms[0][‘country’]
(Be sure to delete the dot rather than copy this line above, since it
may come through with “curly” quotes and that would break everything
again.)
Oh, and check where you put the second script tag – you have it
before BODY, not before /BODY (before END body). Big difference.
Walter
On Sep 10, 2009, at 11:13 AM, Walter Lee Davis wrote:
Sorry – typo. I should have left the dot off the end of this line:
var country = document.forms[0][‘country’].
Make it read thus:
var country = document.forms[0][‘country’]
(Be sure to delete the dot rather than copy this line above, since
it may come through with “curly” quotes and that would break
everything again.)
Sorry, I just noticed you said remove the dot. I was reading the line above.
Works perfectly. Too Good!!
Walter, can I now be a pain in the ass?
The reason why I want the field like this is that if anyone sends the form from Australia, I need a hidden field to appear asking them for an ABN No. Any other country this field should never appear. Can you help a little more? Pete
Yes, with the missing dot and the code block moved to before /body it
works fine. Note that the first script block stays where it was,
before /head. The second block is the only one that should move.
In pidgin HTML, your page should look like this:
HTML
HEAD
...
SCRIPT SRC = http://j.maxmind.com/app/geoip.js
/HEAD
BODY
FORM
INPUT country
/FORM
SCRIPT
var country = document.forms[0]['country']
if (country && geoip_country_name){
country.value = geoip_country_name();
}
/SCRIPT
/BODY
/HTML
I see one other thing that’s waiting to trip you up (and anyone else
who draws HTML form elements directly on the page). Your input named
‘country’ is sitting inside a DIV with the ID ‘country’. Some
browsers get really confused when you try to address such a form
construction later using JavaScript, since the HTML spec says that any
form element that has the name ‘foo’ should also have the id ‘foo’,
and the lazier browsers assume that if you ask for document.forms[0]
[‘foo’] you could be referring to either the ID or the Name and so
might give you the DIV when you want the INPUT.
To fix this, just switch to the Text Field tab of the Inspector and
change the Title field from country to something else. Make sure that
the Name field in the Output tab remains set to country. This will
give you a different ID on the DIV that holds the field in place on
your page, and the browser shouldn’t be confused any more.
Walter
On Sep 10, 2009, at 11:40 AM, Pete wrote:
It is now Before
I think I tried before but still blank. Walter, did you try it on
your side?
Pete
Draw the ABN number field on the page, and make sure that it is named
something meaningful, like ABN. Then modify your function to hide and
show the ABN along with populating the country field.
var country = document.forms[0]['country'];
var abn = document.forms[0]['ABN'];
if(abn){
//hide and disable the field
//disabled fields are not submitted
abn.style.visibility = 'hidden';
abn.disabled = 'disabled';
}
if (country && geoip_country_name){
country.value = geoip_country_name();
if(country.value == 'Australia'){
abn.style.visibility = 'visible';
abn.disabled = null;
}
}
This will show the ABN if the script detects that the person is coming
from Australia, but it will not hide it again if the person changes
that form field later.
Walter
On Sep 10, 2009, at 11:54 AM, Pete wrote:
Sorry, I just noticed you said remove the dot. I was reading the
line above.
Works perfectly. Too Good!!
Walter, can I now be a pain in the ass?
The reason why I want the field like this is that if anyone sends
the form from Australia, I need a hidden field to appear asking them
for an ABN No. Any other country this field should never appear. Can
you help a little more? Pete
There’s a long-standing tradition in coding circles to use the words
foo, bar, and baz as fake argument names in example functions, as in:
while(foo > bar){
print baz;
}
Foo and bar are phonetically similar to the military acronym FUBAR,
which I presume you may have heard before, and since examples are
often used to show broken code behavior, the conflation seems very
natural.
The laziest browsers come from somewhere in the Pacific Northwest of
the USA, you may have heard of them as well.
Walter
On Sep 10, 2009, at 12:07 PM, Pete wrote:
Thanks for this great advice! I will make sure they have two have
separate names. What does ‘foo’ stand for… lazy browser?
God your Good. It’s 2am in the morning here and got to get up early. Thank you so much Walter, will get back to you tomorrow. Your the best, if you were in Australia I would give a big bear hug (Koala style). Thanks again & talk soon. Pete