pass variable from javascript to php and write to database

0

I need to get the alert variables and move to PHP, and then write to the bank, the bank record and quiet, what is 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>
    
asked by anonymous 05.02.2018 / 16:54

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 technology widely used today and that is in evidence because it makes your applications much more dynamic and with greater capabilities of answers.

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 variables that you will send to php success is what will be executed success case in the request and fail is what will be executed if the request fails

What do I do in the PHP page?

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

so it will populate the variables if they are set via   post

    
05.02.2018 / 17:12