Javascript 地理位置保存到PHP文件

Javascript 地理位置保存到PHP文件,javascript,php,html,Javascript,Php,Html,我有以下问题,由于某些原因,地理位置数据没有保存在文本文件中 <script> window.onload=function() { if(navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); } else { alert("Geolocation is not supported by this browser.");

我有以下问题,由于某些原因,地理位置数据没有保存在文本文件中

<script>
window.onload=function() {
    if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else {
        alert("Geolocation is not supported by this browser.");
    }
}
function showPosition(pos){
   $.POST('saver.php', {
        'lat':pos.coords.latitude,
        'lng':pos.coords.longitude
   }, function(res){
       console.log(res);
   });
}
</script>
saver.php:

<?php
   print_r($_POST);
   $a = fopen("save.txt", "a");
   fwrite($a,"Location: $_POST[lat],$_POST[lng]\n*******************\n");
   fclose($a);
?>
替换:

fwrite($a,"Location: $_POST[lat],$_POST[lng]\n*******************\n");
与:

fwrite($a,"Location: ".$_POST['lat'].",".$_POST['lng']."\n*******************\n");

JavaScript区分大小写

因此,$.POST未定义,而$.POST已定义。

使用$.POST小写字母代替$.POST。你的剧本写得很好。此外,请确保已将jquery包含到脚本中:

<script src="jquery.js"></script>

之后,您需要检查浏览器的地理定位支持,并确保允许浏览器使用地理定位。

请发布saver.php的相关部分。执行脚本后,save.txt中有什么内容?fwrite可能应该有$_post['lat']而不是$_post[lat]-与lng相同。我已经做了​​更改,但仍然相同,不会创建文本文件。