我在将变量发布到php脚本并在不刷新页面的情况下返回结果时遇到问题。php脚本koord.php
经过测试,运行良好。
这是我的 js 代码(adresa
, mjest
o 和coords
是文本输入框):
$(document).ready(function () {
$('#coord_click').click(function () {
provjera();
});
});
function provjera() {
var adresa = $('#adresa').val();
var mjesto = $('#mjesto').val();
var puna_adresa = adresa + " " + mjesto;
$.post("koord.php", { puna_adresa: puna_adresa },function (result) {
$('#coords').val(result);
});
}
koord.php:
$puna_adresa = $_GET['puna_adresa'];
function getCoordinates($address){
$address = str_replace(" ", "+", $address);
$url = "maps.google.com/maps/api/geocode/…";
$response = file_get_contents($url);
$json = json_decode($response,TRUE);
return ($json['results'][0]['geometry']['location']['lat'].",".$json['results'][0]['geometry']['location']['lng']);
}
echo getCoordinates($puna_adresa);
完整的源代码在这里: http: //pastebin.com/u/bradetic
谢谢!