Get City and State by ZipCode
http://stackoverflow.com/questions/10340685/get-city-name-from-zip-code-google-geocoding
The HTML:
<input id="shipfrom_zip" name="shipfrom_zip" type="text" required="" placeholder="33166">
<input type="hidden" id="shipfrom_city" name="shipfrom_city">
<input id="shipto_zip" name="shipto_zip" type="text" required="" placeholder="33167">
<input type="hidden" id="shipto_city" name="shipto_city">
To call the function below first get the input field values where the zip codes are
stored, then pass the input field ID of the new input fields where it will be populated
address1 = document.getElementById("shipfrom_zip").value;
address2 = document.getElementById("shipto_zip").value;
// address1 = value from input field, second_parameter = the new input field ID
codeAddress( address1, 'shipfrom_city' );
codeAddress( address2, 'shipto_city' );
<script>
// Function that accepts a zipcode and an input ID field name
function codeAddress( zipcode, inputid ) {
geocoder = new google.maps.Geocoder();
// var address = document.getElementById('shipfrom_zip').value;
geocoder.geocode({ 'address': zipcode }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
for (var component in results[0]['address_components']) {
for (var i in results[0]['address_components'][component]['types']) {
if (results[0]['address_components'][component]['types'][i] == "administrative_area_level_1") {
// uncomment the state line to grab a state value
// state = results[0]['address_components'][component]['long_name'];
//alert(results[0]['address_components'][1]['long_name'] + ' , ' + state);
console.log(results[0]['address_components'][1]['long_name'] );
document.getElementById(inputid).value = results[0]['address_components'][1]['long_name'] ;
}
}
}
} else {
console.log('Invalid Zipcode');
}
});
}
</script>