pass variable javascript to php and write to database

Asked

Viewed 462 times

0

I need to take the variables of Alert and go to PHP, and then record in the bank, the bank recording and quiet, what not getting 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>

1 answer

3


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?

The Ajax (Asynchronous Javascript and XML) is a widely used technology that is in evidence because it makes your applications much more dynamic and responsive.

Since you are 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 the request, get or post data is the javascript variable Voce sent to php success is what will be executed if successful in the request and fail is what will be executed if the request fails

What I do on the PHP page?

$latitude = (isset($_POST['latitude '])) ? $_POST['latitude '] : "";
 $longitude = (isset($_POST['longitude '])) ? $_POST['longitude '] : "";

so it will fill in the variables if they have setadas via post

  • 1

    Thanks, it worked, I adapted to what I needed here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.