基于具有Geoplugin的Country的自动下拉列表

时间:2013-12-02 08:45:17

标签: jquery geoip

  <?PHP
    function visitor_country()
    {
        $client  = @$_SERVER['HTTP_CLIENT_IP'];
        $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
        $remote  = $_SERVER['REMOTE_ADDR'];
        $result  = "Unknown";
        if(filter_var($client, FILTER_VALIDATE_IP))
        {
            $ip = $client;
        }
        elseif(filter_var($forward, FILTER_VALIDATE_IP))
        {
            $ip = $forward;
        }
        else
        {
            $ip = $remote;
        }
     $ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=196.1.1.0"));
        if($ip_data && $ip_data->geoplugin_countryName != null)
        {
            $result = $ip_data->geoplugin_countryName;
        }

        return $result;

    }

<select name="country" id="country">
<option value="">--</option>
<option  data-dialcode="91" value="India">India</option>
<option  data-dialcode="81" value="Japan">Japan</option>
</select>

我想基于具有geoplugin的国家/地区自动选择选项。 例如$ result =“India”;我需要印度的默认选择

1 个答案:

答案 0 :(得分:1)

我使用以下代码找到了答案

<script language="JavaScript" src="http://www.geoplugin.net/javascript.gp" type="text/javascript"></script>  
  <script type="text/javascript">
jQuery(document).ready(function($) {
    alert("Your location is: " + geoplugin_countryName() + ", " + geoplugin_region() + ", " + geoplugin_city());
    var country = geoplugin_countryName();
   $("#country").val(country).attr('selected',true); 
   });
   </script>