-2

how to get user location based on IP address using javascript ? how to display checkbox if user is coming from other country and hide the checkbox if the user is not coming from other country ?

vreddy
  • 1
  • 3
  • 1
    https://stackoverflow.com/questions/3489460/how-to-get-visitors-location-i-e-country-using-geolocation – null Mar 27 '18 at 17:34

1 Answers1

0

You can get the user's country name using ipapi API

https://ipapi.co/json/

Response

{
    "ip": "103.209.196.6",
    "city": "Dhaka",
    "region": "Dhaka Division",
    "region_code": "C",
    "country": "BD",
    "country_name": "Bangladesh",
    "continent_code": "AS",
    "postal": "1000",
    "latitude": 23.7231,
    "longitude": 90.4086,
    "timezone": "Asia/Dhaka",
    "utc_offset": "+0600",
    "country_calling_code": "+880",
    "currency": "BDT",
    "languages": "bn-BD,en",
    "asn": "AS134180",
    "org": "Md. Shariful Islam T/A BRISK SYSTEMS"
}

so from the response, you got country_code and country_name you can use country_code/country_name.

Code

$.getJSON('https://ipapi.co/json/', function(result) 
{
   $("input[name='country']").each( function () 
   {
     if(result.country_name ==$(this).val())
     {
        $(this).parent().hide();
      }
   }
)});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

<div >
    <label><input type="checkbox" name="country" value="United States"> United States</label>
    <label><input type="checkbox" name="country" value="Bangladesh">Bangladesh</label>
</div>
Mohammad Ali Rony
  • 4,695
  • 3
  • 19
  • 33