pass variable javascript to php and record in the database
-
I need to pick up the alert variables and move to PHP, and then record in the bank, the bank recording and quiet, which does not get it and send to test page
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
setInterval("localizarUsuario()", 3000);function localizarUsuario(){
if (window.navigator && window.navigator.geolocation) {
var geolocation = window.navigator.geolocation;
geolocation.getCurrentPosition(sucesso, erro);
} else {
alert('Geolocalização não suportada em seu navegador.')
}
function sucesso(posicao){
console.log(posicao);
var latitude = posicao.coords.latitude;
var longitude = posicao.coords.longitude;alert(latitude + ' - ' + longitude )
// $.get( "teste.php?latitude="+latitude+"&longitude"+longitude );
}
function erro(error){
console.log(error)
}}
</script>
-
use ajax for this:
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
setInterval("localizarUsuario()", 3000);function localizarUsuario(){
if (window.navigator && window.navigator.geolocation) {
var geolocation = window.navigator.geolocation;
geolocation.getCurrentPosition(sucesso, erro);
} else {
alert('Geolocalização não suportada em seu navegador.')
}
function sucesso(posicao){
console.log(posicao);
var latitude = posicao.coords.latitude;
var longitude = posicao.coords.longitude;alert('Aqui iremos começar nossa requisição ajax');
alert(latitude + ' - ' + longitude )//ajax aqui $.ajax({ type: "POST", url: "url_do_arquivo_que_quero_enviar_os_valores.php", data: { latitude = latitude, longitude = longitude }, success: function (retorno) { console.log('Deu certo'); }, error: function(data) { console.log('Deu erro'); } });
}
function erro(error){
console.log(error)
}}
</script>
What is ajax?
O http://www.linhadecodigo.com.br/artigo/3585/ajax-basico-introducao.aspx#ixzz56FaMhckV (Asynchronous JavaScript and XML) is a technology that is currently widely used and that is quite evident because it makes your applications much more dynamic and with higher response capabilities.
Since using jQuery Ajax would be the best solution:
//ajax aqui
$.ajax({
type: "POST",
url: "url_do_arquivo_que_quero_enviar_os_valores.php",
data: { latitude = latitude, longitude = longitude },
success: function (retorno) {
console.log('Deu certo');
},
error: function(data) {
console.log('Deu erro');
}
});
url
will be what file will be called
type
is the type of request,get
orpost
data
is the variables of javascript you send pro php
success
is what will be executed if successful in the request
andfail
is what will be executed if the request failsWhat do I do on the PHP page?
$latitude = (isset($_POST['latitude '])) ? $_POST['latitude '] : "";
$longitude = (isset($_POST['longitude '])) ? $_POST['longitude '] : "";
so it will fill the variables if they have set via
post