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!