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

173
Visualizações
How to fill a HTML SELECT based on user Geolocation

I'm trying to create a simple HTML form with a Select in which the options given are based on the user's Geolocation. The Select should show the options based on the distance between you and the, already in database defined location. The closest location should appear first.

I figured out how to get the latitude and longitude using some Javascript. This is what I have now, in the end, I'm trying to send the 2 values (latitude and longitude) using AJAX to a small PHP file where I save those values into SESSIONS for later use.

          <script type="text/javascript">
        var x = document.getElementById("demo");
        function getLocation() {
        if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(showPosition);
        } else {
          x.innerHTML = "Geolocation is not supported by this browser.";
        }
      }

      function showPosition(position) {
        var Location_LAT = position.coords.latitude;
        var Location_LONG =  position.coords.longitude;

        $.ajax({
            type:'POST',
            url:'getLocation.php',
            data:'latitude='+Location_LAT+'&longitude='+Location_LONG
        });
      }
      </script>

In my PHP file below, i receive the send values from AJAX and store them into 2 SESSIONS.

session_start(); if(!empty($_POST["latitude"]) && !empty($_POST["longitude"])){
$_SESSION["Latitude"] = $_POST["latitude"];
$_SESSION["Longitude"] = $_POST["longitude"];

}

After I've stored the values in SESSIONS I recall those values in a 3rd PHP file where I add them to an MYSQL query and receive results from a database. I've tried everything yet for some reason every time I check the SQL query the values are ".

My question now is, how do I get users Geolocation and pass the 2 coordinates to a PHP file where I can store/use them later?

Thanks in advance!

over 4 years ago · Santiago Trujillo
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