在我的PHP頁面,我存儲用戶城市,當他來到我的網站。谷歌API JavaScript代碼裏面的PHP
這裏是我從網上獲得的谷歌API。
<script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAAp04yNttlQq-7b4aZI_jL5hQYPm-xtd00hTQOC0OXpAMO40FHAxQMnH50uBbWoKVHwgpklyirDEregg"></script>
<script type="text/javascript">
if(google.loader.ClientLocation)
{
visitor_lat = google.loader.ClientLocation.latitude;
visitor_lon = google.loader.ClientLocation.longitude;
visitor_city = google.loader.ClientLocation.address.city;
visitor_region = google.loader.ClientLocation.address.region;
visitor_country = google.loader.ClientLocation.address.country;
visitor_countrycode = google.loader.ClientLocation.address.country_code;
document.getElementById('yourinfo').innerHTML = '<p>Lat/Lon: ' + visitor_lat + '/' + visitor_lon + '</p><p>Location: ' + visitor_city + ', ' + visitor_region + ', ' + visitor_country + ' (' + visitor_countrycode + ')</p>';
}
else
{
document.getElementById('yourinfo').innerHTML = '<p>Whoops!</p>';
}
</script>
我需要將它作爲PHP變量存儲。有人能告訴我如何在php頁面中使用它嗎?
我知道傳統的方式echo
所有<html> to </html>
並把上面的代碼放在裏面。但想知道其他更好的方法嗎?
我也不知道如何將一個javascript變量賦值給一個php值。
你可以做到這一點通過Ajax:http://api.jquery.com/jQuery.ajax/然後在服務器上,保存它們的位置... – Leonardo
如果你問如何運行這個JavaScript代碼放在'.php'文件中,你可以簡單地將它放在'<?php'和'?>'標籤之外。 – Charles
可能的重複[參考:爲什麼我的Javascript中的PHP(或其他服務器端)代碼無法工作?](http://stackoverflow.com/questions/13840429/reference-why-does-the-php-or-其他的服務器端代碼 - 在 - 我的JavaScript - 不WOR) –