Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

120
Visualizações
Display coordinates value in input text after button clicked

help me with this. I have a javascript to get current user coordinates (latitude & Longitude) by using a botton. So, what I want is when clicked the button, it will get the coordinates and display in the two input text which is one for latitude and one for longitude. Before this, I just display it in <p> element.

HTML & PHP:

<div class="mb-3"><input class="border rounded-0 form-control" type="text" name="business_latitude" id="" placeholder="Latitude" style="font-family: Roboto, sans-serif;" value="<?php echo $bizLat; ?>"></div>
<div class="mb-3"><input class="border rounded-0 form-control" type="text" name="business_longitude" id="" placeholder="Longitude" style="font-family: Roboto, sans-serif;" value="<?php echo $bizLong; ?>"></div>
<p id="lati"></p>
<p id="longi"></p>

Javascript:

var x = document.getElementById("lati");
    var y = document.getElementById("longi");

    function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
    }

    function showPosition(position) {
    x.innerHTML = "Latitude: " + position.coords.latitude;
    y.innerHTML ="Longitude: " + position.coords.longitude;
    }

<?php echo $bizLat; ?>, <?php echo $bizLong; ?> here is where they get saved coordinates in database. I just want to edit the latitude and longitude by pressing the button.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

you can add event listener for a button

    const btn = document.getElementById("yourButton");
    const x = document.getElementById("lati");
    const y = document.getElementById("longi");
    btn.addEventListener("click", function onClick(event) {
        document.getElementById("input_latitude").value = x.innerHTML;
        document.getElementById("input_longitude").value = y.innerHTML;
    });
about 4 years ago · Juan Pablo Isaza Relatório

0

You cannot change the values of php variables using javascript since it run on client side.

You can use document.cookie to store values of coordinates and then access them in your php script. To store your values in cookie update your js script as:

//declare two variables to store your values
var latitude;
var longitude;
//now assign coordiates to these var in showPosition function or where it is possible.

var x = document.getElementById("lati");
    var y = document.getElementById("longi");

    function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
    }

    function showPosition(position) {
    x.innerHTML = "Latitude: " + position.coords.latitude;
    y.innerHTML ="Longitude: " + position.coords.longitude;
    
    //assigning coordinates
    latitude=position.coords.latitude;
    longitude=position.coords.longitude;
    }
    
    //add a cookie with these variables as
    document.cookie="latitude="+latitude;
    document.cookie="longitude="+longitude;
    
    //to ensure cookies are set
    console.log(document.cookie);

Then in php script

$latitude = $_COOKIE['latitude'];
$longitude = $_COOKIE['longitude'];
<!--you can use these values to update your database table as well ––>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda