auto fill for state city using zipcode
Hi i have a zipcode field when user enters a 5/9 digit zip it should auto populate the State and city fields.
Is 开发者_StackOverflowthere any thing like this usin javascript or JQuery???
Thanks
Using the Javascript Google Maps V3 API:
You need to reference this:
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
Do this:=
var zip = <yourzipcode>;
var lat;
var lng;
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': zip }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
geocoder.geocode({'latLng': results[0].geometry.location}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
var loc = getCityState(results);
}
}
});
}
});
function getCityState(results)
{
var a = results[0].address_components;
var city, state;
for(i = 0; i < a.length; ++i)
{
var t = a[i].types;
if(compIsType(t, 'administrative_area_level_1'))
state = a[i].long_name; //store the state
else if(compIsType(t, 'locality'))
city = a[i].long_name; //store the city
}
return (city + ', ' + state)
}
function compIsType(t, s) {
for(z = 0; z < t.length; ++z)
if(t[z] == s)
return true;
return false;
}
This all returns a string containing the city and state in this format , but you can adjust this to your needs.
Google says yes: http://bassistance.de/2009/03/03/jquery-snippet-autocomplete-city-based-on-zip-code/
Found an article on Css-tricks.com, functionality is implemented using Ziptastic http://css-tricks.com/using-ziptastic/
$.ajax({
url: "http://zip.elevenbasetwo.com",
cache: false,
dataType: "json",
type: "GET",
data: "zip=" + el.val(),
success: function(result, success) {
$(".fancy-form div > div").slideDown(); /* Show the fields */
$("#city").val(result.city); /* Fill the data */
$("#state").val(result.state);
$(".zip-error").hide(); /* In case they failed once before */
$("#address-line-1").focus(); /* Put cursor where they need it */
},
error: function(result, success) {
$(".zip-error").show(); /* Ruh row */
}
});
All answers are appreciated, If you are don't want to get into more of complexity of code, here is a solution for you.
First Step:
download latest country zip code Excel file and locate that excel file under root folder.(You can download it here)
https://data.gov.in/catalog/all-india-pincode-pirectory#web_catalog_tabs_block_10)
Second Step:
Call ajax function where is your form
<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type=”text/javascript”><br>
jQuery(document).ready(function(){
jQuery(‘.postcode‘).blur(function(){ //.postcode class of zipcode text field
var s = jQuery(this).val();
jQuery.ajax({
type: ‘POST’,
url:“http://www.sample.com/postcode.php“, //file which read zip code excel file
dataType: “json”, //is used for return multiple values
data: { ‘s’ : s },
success: function(data){
try {
jQuery(“.state“).val(data.state); //region-class of state text field
jQuery(“.city“).val(data.dist);//city-class of city text filed
} catch (e) {
alert(e.Message);
}
},
error:function (xhr, status, err){
alert( “status=” + xhr.responseText + “, error=” + err );
}
});
});
});
</script>
This Ajax function will call “http://www.sample.com/postcode.php” file
Under postcode.php file we have to read excel file and return state and city value
postcode.php
<?php
extract ($_POST);
$s=$_POST[‘s’]; //get value from ajax
$filename=”zip.csv“; //zipcode csv file(must reside in same folder)
$f = fopen($filename, “r”);
while ($row = fgetcsv($f))
{
if ($row[1] == $s) //1 mean number of column of zipcode
{
$district=$row[3]; //3- Number of city column
$state=$row[4]; //4-Number of state column
break;
}
}
fclose($f);
echo json_encode(
array(“dist” => $district,
“state” => $state,
“zip” => $s)
); //Pass those details by json
?>
That’s All, Enjoy
精彩评论