0

I am trying to post latitude and longitude values to a php file where I can store them in a variable and then save it into a database. But I am having trouble passing the values from js to the php file. Below I am posting snippets. The javascript is in the same html file. I have also included the jquery file in the head.

function showPosition() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var latitude = position.coords.latitude;
      var longitude = position.coords.longitude;
      $.ajax({
        type: 'post',
        url: 'a.php',
        data: {
          latitude: latitude,
          longitude: longitude
        },
        success: function(data) {
          console.log(data);
        }
      });
    });
  } else {
    var latitude = "";
    var longitude = "";
  }
}
<body onload="showPosition();"></body>

Now in the file "a.php" I am trying to store these values in variables.

$lat = $_POST['latitude'];
$long = $_POST['longitude'];
echo $lat;
echo $long;

But the values are not getting passed from the javascript to the php. Where am I going wrong? Please help.

nick zoum
  • 7,216
  • 7
  • 36
  • 80
Sachin Menon
  • 69
  • 11

0 Answers0