Select Country Drop Down based on country location with flag with PHP

Friday, September 2, 2011

Selecting country name from country drop down based on the visitors country location will increase visitors user friendliness for your web forms, Let us create How to create Auto Select Country Name based on visitors location based on Country Geo location and  IP Address. When Loading a page detect country name and locate a country flag based on country location.




Create a Free Account for IP to Location API from http://ipinfodb.com/ip_location_api.php  to get a API Key for IP to Location. and get the Country Flag from ip2Location database.
using the URL: http://www.ip2location.com/images/country/{ COUNTRY SHORT CODE} .gif

and Process the code as per the following:



<?php

// CountryList as Array 
// Country ShortCode as Key Country Name as Value;
$world_countires=array('AF'=>'Afghanistan (افغانستان)');
// Note: Download Full Country Array from Download Link from end of this Article
// 

function xml2array($xml) {
  $arXML=array();
  $arXML['name']=trim($xml->getName());
  $arXML['value']=trim((string)$xml);
  $t=array();
  foreach($xml->attributes() as $name => $value) $t[$name]=trim($value);
  $arXML['attr']=$t;
  $t=array();
  foreach($xml->children() as $name => $xmlchild) $t[$name]=xml2array($xmlchild);
  $arXML['children']=$t;
  return($arXML);
}

$xml = simplexml_load_file('http://api.ipinfodb.com/v2/ip_query.php?key=2338e43ca0672d2f259e23ef35b0535962a1d4ce47176953353e60c4f31d2f44&ip='.$_SERVER['REMOTE_ADDR']);
$xmlArray=xml2array($xml);

$country_code=$xmlArray['children']['CountryCode']['value'];

// Check if Local
if($country_code=="RD")
$country_code="US";


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Auto Select Country Dropdown based on Location : Demo</title>
<style type="text/css">
<!--
body,td,th {
	font-family: Verdana, Geneva, sans-serif;
	font-size: 11px;
	color: #333;
}
body {
	margin-left: 20px;
	margin-top: 20px;
}
-->
</style></head>

<body>

<h2>PHP Auto Populate Country Name in Country Drop Down List with Flag</h2>
<table border="0" cellspacing="0" cellpadding="5">
  <tr>
    <td>    
<script type="text/javascript">
function setFlag(country_code)
{
	document.getElementById('flag').src="http://www.ip2location.com/images/country/"+ country_code +".gif";
}
</script>

<select name="country_name" onchange="setFlag(this.value)" style="font-family:Verdana, Geneva, sans-serif; width:200px; font-size:15px; border:1px solid #999;">
<option value="">Select Country</option>
<?php
foreach($world_countires as $short_code=>$country_name)
{
	if($country_code==$short_code)
	echo "<option value=\"$short_code\" selected=\"selected\">$country_name</option>";
	else
	echo "<option value=\"$short_code\">$country_name</option>";
}
?>
</select>    
    </td>
    <td><img id="flag" src="http://www.ip2location.com/images/country/<?=$country_code?>.gif" width="30" height="20" /></td>
  </tr>
</table>

</body>
</html>



Download This Script     Live Demo     Download Script

No comments:

Post a Comment