php中的google API javascript代码
在我的php页面中,当用户访问我的站点时,我将其存储在city中 这是我从网上得到的谷歌APIphp中的google API javascript代码,javascript,php,Javascript,Php,在我的php页面中,当用户访问我的站点时,我将其存储在city中 这是我从网上得到的谷歌API <script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAAp04yNttlQq-7b4aZI_jL5hQYPm-xtd00hTQOC0OXpAMO40FHAxQMnH50uBbWoKVHwgpklyirDEregg"></script> <script type="te
<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>
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\u country=google.loader.ClientLocation.address.country;
visitor\u countrycode=google.loader.ClientLocation.address.country\u code;
document.getElementById('yourinfo').innerHTML='Lat/Lon:'+visitor\u Lat+'/'+visitor\u Lon+'位置:'+visitor\u city+'、'+visitor\u region+'、'+visitor\u country+'('+visitor\u countrycode+');
}
其他的
{
document.getElementById('yourinfo')。innerHTML='Whoops!';
}
我需要将其存储为PHP变量。有人能告诉我如何在php页面中使用它吗
我知道用传统的方法将echo
allto
和上面的代码放在里面。但是想知道其他更好的方法吗
我也不知道如何将javascript变量分配给php值
也不知道如何将javascript变量值指定为php值
您不能这样做,因为Javascript在客户端、浏览器和服务器上的php中运行。您还可以通过php中的IP地址轻松获取访问者的位置详细信息:
<?php
require_once("userip/ip.codehelper.io.php");
require_once("userip/php_fast_cache.php");
$_ip = new ip_codehelper();
$real_client_ip_address = $_ip->getRealIP();
$visitor_location = $_ip->getLocation($real_client_ip_address);
$guest_ip = $visitor_location['IP'];
$guest_country = $visitor_location['CountryName'];
$guest_city = $visitor_location['CityName'];
$guest_state = $visitor_location['RegionName'];
echo "IP Address: ". $guest_ip. "<br/>";
echo "Country: ". $guest_country. "<br/>";
echo "State: ". $guest_state. "<br/>";
echo "City: ". $guest_city. "<br/>";
?>
有关详细信息,请转到此处您不能将Javascript变量值分配给PHP变量,因为Javascript在客户端Borwser上运行,而PHP脚本在服务器上运行 但是有一种方法可以使用POST或GET方法将其发送回服务器 您甚至可以使用AJAX/JQuery来实现这一点 使用Jquery的方法之一是最简单的。以下是它的链接: 在服务器上创建一个PHP文件,称之为storecity.PHP
<?php
$city = $_GET['city'];
$user = $_GET['name'];
// Write your code to store the variable $city into the database
?>
您可以通过ajax实现这一点:然后在服务器中保存它们的位置……如果您询问如何在
.php
文件中运行此JavaScript代码,您只需将其放在
标记之外。可能的重复
$.ajax({
type: "GET",
url: "storecity.php",
data: { name: "John", city: "Boston" }
});