0

This problem has already been addressed in other posts and other forums around the network but i don't understand where wrong.
I've this code javascript for find user's position:

if(!!navigator.geolocation){
  navigator.geolocation.getCurrentPosition(geolocalizzami);
}
else {
  alert("Geolocalizzazione non supportata");
}

function geolocalizzami(position) {
  document.getElementById("lon").innerHTML = position.coords.longitude;
  document.getElementById("lat").innerHTML = position.coords.latitude;  
}

and i can print with

<label id='lon' /></label>

and ok. I need of loading these data into database. I know I need to use jquery and ajax. The ajax code into an html page its serv me for find the coordinates and send these data, with the post method, to php page. here i can using $_POST['value'] for work with the data. An example, found in internet, that i use is:

page.html:

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script 
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
</head>
<body>
<script type="text/javascript">
if (navigator.geolocation) {

navigator.geolocation.getCurrentPosition(function(position) {

    $.ajax({
        url:'page2.php',
        data: {
            'lat': position.coords.latitude,
            'lng': position.coords.longitude
        },
        type: 'get',
        success: function (result) {
            // If your backend page sends something back
            alert(result);
        }
    });

  });
}
</script>
</body>
</html>

page2.php:

<?php
echo $lat = $_POST['lat'];
echo $lng = $_POST['lng'];
?>

Why $lat and $lng are empty? thanks in advance.

1 Answer 1

1

Change

echo $lat = $_POST['lat'];
echo $lng = $_POST['lng'];

to

echo $lat = $_GET['lat'];
echo $lng = $_GET['lng'];

Or

type: 'get',

to

type: 'post',

You are mixing POST and GET which are 2 different things in PHP. I would recommend to use tool like postman to see what is happening. This sometimes gives you more information about what is going on.

For testing purpese you could change the $_POST to $_GET and try:

$.get( "page2.php?lat="+position.coords.latitude+"&lng="+position.coords.longitude, function( data ) {
 console.log(data)
});

Also it's more common to use

$lat = $_GET['lat'];
$lng = $_GET['lng']; // POST

echo $lat;
echo $lng;

Or just

echo $_GET['lat'];
echo $_GET['lng']; // or POST
Sign up to request clarification or add additional context in comments.

4 Comments

The mixing was my inattention, but thanks for pointing to me. Anyway I corrected using the post method on both sides but I keep seeing nothing.
They are still the first weapons with javascript, but I put your code in place of $ .ajax ({...}) (right?). With both get and post, but I keep seeing nothing.
Are you sure page2.php is at the location you expect it to be? Have you checked that position.coords.latitude and position.coords.longitude actually contain a value?
As I said before, I'm recently on javascript, in fact, a first mistake I made was to put the ajax function in page.html and send the data to page2.php. Now I put everything together but it only works when the case of success prints with 'document write (date)'. in fact, having not put a button (I need that I'm doing it all tot), loop the page on itself, displaying the rescheduled I want. if I type with 'alert' or 'consol.log' it shows me the code in full.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.