2

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= ?????
?>
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Dan Stern
  • 2,155
  • 8
  • 26
  • 38
  • possible duplicate of [How to pass JavaScript variables to PHP?](http://stackoverflow.com/questions/1917576/how-to-pass-javascript-variables-to-php) – McGarnagle Oct 08 '12 at 05:27
  • @dbaseman Given the antique answer from the duplicate, perhaps it should be the other way around? :) – Ja͢ck Oct 17 '12 at 04:15

4 Answers4

3

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.

Cesar
  • 3,519
  • 2
  • 29
  • 43
1

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

Tom Gullen
  • 61,249
  • 84
  • 283
  • 456
0

This will convert js variable to php variable

<script>
function sud(){

javavar=document.getElementById("text").value;  

document.getElementById("rslt").innerHTML="<?php 
$phpvar='"+javavar+"'; 
echo $phpvar.$phpvar;?>";
}

function sud2(){
document.getElementById("rslt2").innerHTML="<?php 
echo $phpvar;?>";
}

</script> 
<body>
<div id="rslt">
</div>

<div id="rslt2">
</div>
<input type="text" id="text" />
<button onClick="sud()" >Convert</button>
<button onClick="sud2()">Once Again</button>

</body>

Demo: http://ibence.com/new.php

0

You can POST through a form or pass it in the URL if you're doing it on page transition, then just use $_POST or $_GET to receive the variable.

If you need it done seamlessly then you might want to look at using AJAX. TIZAG AJAX Tutorial

Kenny Linsky
  • 1,726
  • 3
  • 17
  • 41
John
  • 477
  • 1
  • 7
  • 16