我正在尝试编写一个应用程序,每分钟左右将用户的地理位置发送到 MYSQL 数据库。这是我现在拥有的代码,但它不起作用。我需要如何更改它才能使其正常工作?
JS:
<!DOCTYPE html>
<html>
<head>
<script src="js/jquery.js"></script>
</head>
<body>
<script>
setInterval ( "onPositionUpdate()", 10000 );
function onPositionUpdate(position)
{
var lat = position.coords.latitude;
var lng = position.coords.longitude;
jQuery.ajax({ type: "POST",
url: "myURL/location.php",
data: 'x='+lat+ '&y='+lng ,
cache: false,
});
}
if(navigator.geolocation)
navigator.geolocation.watchPosition(onPositionUpdate);
else
alert("navigator.geolocation is not available");
</script>
</body>
</html>
和 PHP:
<?php
include 'config.php';
// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
// new data
$x = @$_POST['x'];
$y = @$_POST['y'];
// query
$sql = "update locations set x=?, y=? where username = asd";
$q = $conn->prepare($sql);
$q->execute(array($x), ($y));
?>
而且我想这不是发送多个变量的正确方法,是吗?Firebug 的控制台向我展示
missing } after property list
[Break On This Error]
data: 'x='+lon+'&y='+lat;
当我只使用一个值时,它只会将该变量发布到服务器一次,然后控制台给出:
position is undefined
[Break On This Error]
var lat = position.coords.latitude;