how can I save my coordinates in mysql using "navigator.geolocation.getCurrentPosition"

2

Hello that such a friend turns out to be very stuck with this problem that happened to me in the work I want to save the coordinates latitude and longitude in php variable and then proceed to insert them in mysql but I can not make the javascript variable pass as is the php here I leave my code to analyze it if they could help me would be great GREETINGS FROM MEXICO

    <html>
<head>
<script type='text/javascript'>

navigator.geolocation.getCurrentPosition(fn_ok, fn_error);

function fn_error(){

alert('Error');
}

function fn_ok(respuesta){

var lat = respuesta.coords.latitude;
var lon = respuesta.coords.longitude;


global=lat+' '+lon;
alert(global);
}

var global='hol';
</script>
</head>
<body>
<?php
$variablePHP = 
"<script type='text/javascript'>; 
var id=global;
  document.writeln(id) 
  alert(id);
  </script>";
echo $variablePHP;
?>
</body>
</html>

Here is the code but I can not get the latitude and longitus saved in the php variables if someone knows and could tell me how to solve the problem would help me a lot of thanks in advance to everyone.

    
asked by Anahi Rosas Trejo 19.08.2016 в 20:31
source

1 answer

1

One of the options you can do is send the coordinates by the URL and process them in another file:

//En Javascript:
navigator.geolocation.getCurrentPosition(fn_ok, fn_error);

function fn_error(){

alert('Error');
}

function fn_ok(respuesta){

var lat = respuesta.coords.latitude;
var lon = respuesta.coords.longitude;

global="lat="+lat+'&lon='+lon;
var urlDestino = "http://fredyfx.com/index.cshtml?"+global;
//Esto genera: http://fredyfx.com/index.cshtml?lat=-8.1167518&lon=-79.0371252
alert(global);
window.open(urlDestino, '_self');
}

Getting the variables in PHP

<?php $miLatitud = $_GET["lat"]; ?>
<?php $miLongitud = $_GET["lon"]; ?>
    
answered by 19.08.2016 / 21:19
source