3

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

"Notice: Undefined index: "

The jquery variable data is always empty

... 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
$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 !';
}
?> 
Kalasni
  • 45
  • 4
  • this might be helpful to you: output all the post data on the server end and see if you're receiving the data: [output all POST data](http://stackoverflow.com/questions/6334830/php-possible-to-automatically-get-all-posted-data). Also the POST data needs to be in this format `{key1: 'value1', key2: 'value2'}` so for you, it's `{data: 'value1',}` if on the php side you're using `$_POST['data']` – zer0bit Apr 16 '13 at 13:18

2 Answers2

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:

</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)
Kalasni
  • 45
  • 4
0

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

data: { lat: latitude, lon: longitude }

and in checklo.php

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

Thanks

Kalasni
  • 45
  • 4