6
<body>
<script type='text/javascript'>
var lati = '';
var long = '';
var cidade = '';
var estado = '';
var pais = '';
var dadosajax = '';
navigator.geolocation.getCurrentPosition(function (posicao) {
var url = "http://nominatim.openstreetmap.org/reverse?lat=" + posicao.coords.latitude + "&lon=" + posicao.coords.longitude + "&format=json&json_callback=preencherDados";
var script = document.createElement('script');
script.src = url;
document.body.appendChild(script);
lati = posicao.coords.latitude;
long = posicao.coords.longitude;
});
function preencherDados(dados) {
cidade = dados.address.city;
estado = dados.address.state;
pais = dados.address.country;
alert('cordenadas:' + lati + ',' + long + ' Lugar:' + cidade + ',' + estado + ',' + pais);
dadosajax = cidade;
alert(dadosajax);
$.ajax({
type: "POST",
url: "teste6.php",
data: {cidade: cidade}
});
}
;
</script>
</body>
filing cabinet teste6.php
:
<?php
require_once './php/conn_db.php';
$cidade = $_POST['cidade'];
$sql = "INSERT INTO arq011 (codigo,cidade) VALUES (null, :cidade)";
$stmt = Conexao::getInstance()->prepare($sql);
$stmt->bindParam(':cidade', $cidade, PDO::PARAM_STR);
$executa = $stmt->execute();
?>
not saving... what I’m doing wrong?
How far have you checked if the data is being passed? Does ajax send something? The
$_POST
has values? , etc...– Sergio
Sergio, I couldn’t pass data to the test file6.php. Until the Alert(dadosajax) instruction I got results, but nothing within $.ajax({
– Ricardo Copano
The url
url: "teste6.php",
Is that correct? There won’t be a sub-folder?– Sergio
"teste6.php" is in the same folder.
– Ricardo Copano
What gives
echo var_dump($cidade);
?– Sergio
nothing. It seems that it doesn’t even 'call' teste6.php
– Ricardo Copano
What says the "network" tab of the Browser tools?
– Sergio
Uncaught Referenceerror: $ is not defined
– Ricardo Copano
Knuckle
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
in the<head>
page...– Sergio
Sergio, the error no longer appears in the console-network. However, it is still not saved in the bd. Ajax is not 'calling' teste6.php :(
– Ricardo Copano