Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!