I wanted to implement auto-populate city and state from zip code.
I found this link : zipautocomplete
This link is working super. Now I wanted to copy this code so right clicked and chosen view page source and there I found the html and ajax code.
I copied code created index.php on my localhost. I have also copied necessary files like jquery.min.js etc.
Now my code is same as that of link but I am not getting same output.
Here is code that I have copied :
<!doctype html>
<html>
<head>
<title>ZIP code autocomplete test</title>
<link rel="stylesheet" type="text/css" href="css/jquery.autocomplete.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js" type="text/javascript"></script>
<script src="js/jquery.autocomplete.js" type="text/javascript"></script>
<style type="text/css">
body { font-family: Helvetica, Arial, sans-serif; }
.ac_city { font-size: smaller; float: right; width: 70% }
.ac_zip { font-weight: bold; float: left; width: 25%; }
.ac_city, .ac_zip { margin: 0.1em 0; }
</style>
<script type="text/javascript">
$(document).ready(function() {
$("#zip, #city").autocomplete("zip_json.pl", {
minChars: 2,
selectFirst: true,
matchSubset: true,
width: 220,
scrollHeight: 300,
max: 1024,
dataType: 'json',
extraParams: {
zip: function () {
return $("#zip:focus").val();
},
city: function () {
var c = $("#city:focus").val();
return c && c + '%'
}
},
parse: function (data) {
var a = [];
for(var i = 0;i < data.length; i++)
a.push({ data: data[i],
value: data[i].zip,
result: data[i].zip
});
return a;
},
formatItem: function (item) {
return "<span class='ac_zip'>" + item.zip + "</span>" +
"<span class='ac_city'>" + item.city +
", " + item.state + "</span>";
},
});
$("#zip, #city").result(function (event, item) {
$("#zip").val(item.zip);
$("#city").val(item.city);
$("#state").val(item.state);
});
});
</script>
</head>
<body>
<p>Enter a zip code or city and it will start autocompleting with 2
or more chars. Select an item from the list and it will fill in the
zip, city and state.</p>
<form>
<label> ZIP: <input name="zip" id="zip" type="text" size="5" maxlength="5"></label>
<label> City: <input name="city" id="city" type="text" size="20"></label>
<label> State: <input name="state" id="state" type="text" size="2" maxlength="2"></label>
</form>
</body>
</html>
When I checked above code on local, actually the code working correctly but it is not picking correct json response. when I enter 2 numbers in zip field then it should show related zipcode which is showing on that website but on my local browswer it is loading all joson_zip.pl file's json data.
When I checked "NET" output from firebug tool, I found difference in json response of local output and link output.
Can anyone please help me on this ?