使用jquery将地理位置数据保存到html文件

时间:2022-10-22 22:57:28

I'm trying to write the geolocation data to an HTML file, but doesn't work. I always get the following message regarding data:

我正在尝试将地理位置数据写入HTML文件,但不起作用。我总是收到有关数据的以下消息:

"Notice: Undefined index: "

“注意:未定义的索引:”

The jquery variable data is always empty

jquery变量数据始终为空

... what am I doing wrong?

... 我究竟做错了什么?

Thanks in advance.

提前致谢。

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>

<body onload="testResults()">
<script>
function testResults()
{
if (navigator.geolocation)
{
    //Get the current position
    navigator.geolocation.getCurrentPosition(function (position)
    {
        var latitude = position.coords.latitude;
        var longitude = position.coords.longitude;
        //var cadena = "lat=" + latitude + "&lon=" + longitude;
        var cad = latitude + "  " + longitude;

        // do callback                
        $.ajax(
        {
            type: "POST",
            url: "checklo.php",
            //data: "lat=" + latitude + "&lon=" + longitude
            data: cad,
        }).done(function (msg)
        {
            // Only to check it
            alert("Data Saved: " + cad);
        });

    });
}
else
{
 alert("Sorry... your browser does not support the HTML5 GeoLocation API");
}
}

and the code of php file checklo.php

以及php文件checklo.php的代码

<?php
$file = 'geo.html';
$datos = $_POST['data'];

if (isset($datos)) {

  $fp = fopen($file, "a");
  fputs($fp, "</br>$datos </br>");
  flock($fp, 3); 
  fclose($fp);
  //echo 'msg ';
}
else {
  //echo 'msg !';
}
?> 

2 个解决方案

#1


0  

Thanks for you answer

谢谢你的回答

I have checked it with print_r($_POST, true) and with var_export($_POST, true) and I get no output, the only thing I get in the html file is this, a empty array:

我用print_r($ _ POST,true)和var_export($ _ POST,true)检查了它,我得不到输出,我在html文件中得到的唯一的东西就是这个,一个空数组:

</br>Array
(
)
</br></br>Array
(
)
</br></br>Array
(
)

I've tried with both ways of using data but isn't working

我尝试过两种使用数据的方法,但是没有用

 $.ajax(
 {
     type: "POST",
     url: "checklo.php",
     //data: "lat=" + latitude + "&lon=" + longitude ,
     data: 'cad' ,
 }).done(function (msg)

#2


0  

I've been able to solved using the following format in ajax call

我已经能够在ajax调用中使用以下格式解决了

data: { lat: latitude, lon: longitude }

and in checklo.php

并在checklo.php

foreach ($_POST as $key => $value) 
  $dat .= $key . ' -> ' . $value . '<br>';

Thanks

谢谢

#1


0  

Thanks for you answer

谢谢你的回答

I have checked it with print_r($_POST, true) and with var_export($_POST, true) and I get no output, the only thing I get in the html file is this, a empty array:

我用print_r($ _ POST,true)和var_export($ _ POST,true)检查了它,我得不到输出,我在html文件中得到的唯一的东西就是这个,一个空数组:

</br>Array
(
)
</br></br>Array
(
)
</br></br>Array
(
)

I've tried with both ways of using data but isn't working

我尝试过两种使用数据的方法,但是没有用

 $.ajax(
 {
     type: "POST",
     url: "checklo.php",
     //data: "lat=" + latitude + "&lon=" + longitude ,
     data: 'cad' ,
 }).done(function (msg)

#2


0  

I've been able to solved using the following format in ajax call

我已经能够在ajax调用中使用以下格式解决了

data: { lat: latitude, lon: longitude }

and in checklo.php

并在checklo.php

foreach ($_POST as $key => $value) 
  $dat .= $key . ' -> ' . $value . '<br>';

Thanks

谢谢