我無法獲得以下代碼在PHP中工作。我想檢索用戶位置,然後將其發送到我的PHP腳本,以便在服務器上進一步處理。 Php不會檢索數據。
編輯:
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
</head>
<body>
<script>
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
} else {
alert('Geolocation is required for this page, but your browser doesn't support it. Try it with a browser that does, such as Opera 10.60.');
}
function successFunction(position) {
var lat = position.coords.latitude;
var long = position.coords.longitude;
var latlong = {"lat" : lat , "long" : long};
var latlong_encoded = JSON.stringify(latlong);
$.ajax({
url: 'PostTest.php',
type: 'POST',
data: {"userLocation" : latlong_encoded},
success: function(data) {
alert(latlong_encoded);
}
});
}
function errorFunction(position) {
alert('Error!');
}
</script>
</body>
</html>
這裏是PHP代碼:
<?php
$lat[]= array('coord'=>json_decode($_POST["userLocation"]));
print_r($lat);
?>
我只是得到一個空數組。 陣列([0] =>數組([座標] =>))
「的PHP不會檢索數據「如何? – Jan
javascript是好的 - 問題是在Dataprocessing.php中,或者,如果這是DataProcessing.php,你錯誤地認爲發佈到DataProcessing.php會導致php底部的代碼片段什麼都做 –
我已經嘗試了兩種方法。最初有兩種不同的腳本,其中php是Data Processing.php。由於沒有工作,我試圖把它全部放在一個腳本中。我想這是錯誤的做法。雖然沒有工作, – Mdude