How to capture a visitor's country in a form?

  • 15 September 2017
  • 6 replies
  • 35 views

I’m curious. How to capture a visitor’s country in a form (when the visitor fills a form)? Thanks.


6 replies

Userlevel 7
Badge +1

Hey Adrien!

You can use geotargeting to display a visitors location a page using one of our Tips and Scripts here. Let me know if that helps you achieve that functionality!

Custom drop down?

Thanks @Jess. I’ve added a comment over there.

@Alex_Scovell, hi. I wanted a script behind the scene so I do not have to ask the visitor for the information.

@Adrien very slow reply!

This one passes IP address into a hidden form field called ‘IPadd’

<script type="text/javascript">
  
  $.get("https://ipinfo.io", function(response) {
    $("#IPadd").val(response.ip);
    //alert(response.ip);
}, "jsonp");

</script>

And i just found this:

$.get(“https://ipinfo.io”, function(response) {
console.log(response.city, response.country);
}, “jsonp”);

I think if you try something like this (combining the two):

<script type="text/javascript">
      
      $.get("https://ipinfo.io", function(response) {
        $("#IPadd").val(response.country);
        //alert(response.ip);
    }, "jsonp");

    </script>

(i just swapped country in in the place of IP)

It will probably work - you need to add a hidden form field called IPadd - or call it what you like and change this bit: $("#IPadd")

Works like a charm…Thanks @Alex_Scovell

Reply