-
-
Save huglester/d17768a46c5df256b63c to your computer and use it in GitHub Desktop.
auto zip code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author Jokūbas Ramanauskas | |
* @since 2015-03-26 | |
* | |
* Fetches postcode by given address information | |
* | |
* @return bool | |
*/ | |
public function getPostCodeByAddress() | |
{ | |
$city = $this->_getPost('city'); | |
$address = $this->_getPost('address'); | |
if (!empty($city) && !empty($address)) { | |
try { | |
$result = file_get_contents(sprintf('http://postit.lt/data/?address=%s, %s', urlencode($address), urlencode($city))); | |
if (!empty($result)) { | |
$result = json_decode($result, 1); | |
if (!empty($result['total'])) { | |
die(json_encode(array( | |
'status' => 'OK', | |
'zipcode' => array_shift($result['data'])['post_code'], | |
))); | |
} | |
} | |
} catch (Exception $e) { | |
die(json_encode(array( | |
'status' => 'error', | |
))); | |
} | |
} | |
die(json_encode(array( | |
'status' => 'error', | |
))); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('*[data-event="addressChange"]').change(function(e){ | |
var $target = $(this); | |
var $cityField = $($target.data('city-field')); | |
if ($cityField.length > 0) { | |
var $addressField = $target; | |
} else { | |
$cityField = $target; | |
var $addressField = $($target.data('address-field')); | |
} | |
var $zipCodeField = $($target.data('zipcode-field')); | |
var $zipCodeHelpField = $($zipCodeField.data('help-field')); | |
$.ajax({ | |
type: 'POST', | |
async: true, | |
url: $zipCodeField.data('url'), | |
data: { | |
'city': $cityField.children('option:selected').text(), | |
'address': $addressField.val() | |
}, | |
beforeSend: function(){ | |
$zipCodeHelpField.attr('href', "http://postit.lt/paieška/" + encodeURIComponent($addressField.val()) + ", " + encodeURIComponent($cityField.children('option:selected').text())) | |
}, | |
success: function(data){ | |
data = $.parseJSON(data); | |
if (data.status === "OK") { | |
$zipCodeField.val(data.zipcode); | |
} | |
} | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment