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


#1

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


#2

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!


#3

Custom drop down?


#4

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


#5

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


#6

@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")