Skip to content

Instantly share code, notes, and snippets.

@devjosh12
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devjosh12/200c4d360e52805dee0a to your computer and use it in GitHub Desktop.
Save devjosh12/200c4d360e52805dee0a to your computer and use it in GitHub Desktop.
Autopopulate form by entering zipcode for INDIA only
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> </script >
<script type="text/javascript">
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://localhost/autopopulate/insertzip.php", //file which read zip code excel file
dataType: "json", //is used for return multiple values
data: { 's' : s },
success: function(data){
try {
jQuery("#city").val(data.dist);//city-class of city text filed
jQuery("#state").val(data.state); //region-class of state text field
} catch (e) {
alert(e.Message);
}
},
error:function (xhr, status, err){
alert( "status=" + xhr.responseText + ", error=" + err );
}
});
});
});
</script>
<form action="#" method="post">
<p><label for="postcode">POSTCODE</label><br />
<input type="text" name="postcode" id="postcode" value="" /></p>
<p><label for="city">City</label><br />
<input type="text" name="city" id="city" value="" /></p>
<p><label for="region">State</label><br />
<input type="text" name="state" id="state" value="" /></p>
</form>
<?php
//extract ($_POST);
$s=$_POST['s']; //get value from ajax
$filename='zipcodes.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[2]; //3- Number of city column
$state=$row[3]; //4-Number of state column
break;
}
}
fclose($f);
echo json_encode(
array("dist" => $district,
"state" => $state,
"zip" => $s)
); //Pass those details by json
?>
Sr_No Pincode City State ZONE Prepaid COD REVERSEPICKUP
1 110001 DELHI DELHI NORTH Y Y Y
2 110002 DELHI DELHI NORTH Y Y Y
3 110003 DELHI DELHI NORTH Y Y Y
4 110004 DELHI DELHI NORTH Y Y Y
5 110005 DELHI DELHI NORTH Y Y Y
6 110006 DELHI DELHI NORTH Y Y Y
7 110007 DELHI DELHI NORTH Y Y Y
8 110008 DELHI DELHI NORTH Y Y Y
9 110009 DELHI DELHI NORTH Y Y Y
10 110010 DELHI DELHI NORTH Y Y Y
11 110011 DELHI DELHI NORTH Y Y Y
12 110012 DELHI DELHI NORTH Y Y Y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment