Pass a JS variable to a PHP variable

I have a JavaScript value given by Google maps and I need to save it in a MySQL database.

Actually I have the variable

<script>
...
var lugar = results[0].geometry.location;// this gives me a latitud, longitud value, like: -34.397, 150.644
...
</script>

And I need to pass that variable to the PHP variable lugar

<?
$lugar= ?????
?>

You can use jQuery ajax for this, but you need to create another script that save on your database:

<script>
$.ajax({
    url: "save.in.my.database.php",
    type: "post",
    dataType:"json",
    data: {
        lugar: results[0].geometry.location
    },
    success: function(data){
        alert('saved');
    },
    error: function(){
        alert('error');
    }
});
</script>

"save.in.my.database.php" receives a $_POST['lugar'] and you can save on your database.


You will need to pass it via a form submission, cookie, or through a querystring.