Is there any easy way to pass data from JavaScript to PHP without using the Query String Method.
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
}
}
function showPosition(position) {
document.getElementById("getlat").value = position.coords.latitude;
document.getElementById("getlon").value = position.coords.longitude;
}
HTML
<input type="hidden" id="getlat" name="getlat" value="<?php echo $_SESSION['polat']; ?>" />
<input type="hidden" id="getlon" name="getlon" value="<?php echo $_SESSION['polon']; ?>" />
PHP Code
$lat = '-27.486204'; // Should get latitude from JS here.
$long = '152.994962'; // Should get longitude from JS here.
mysqli_query("SELECT * From `table_nme` where `latitude`='.$lat.' AND `longitude`='.$long.'");
I know how to do this in Query String Process but I need it without Query String Process.
You can use ajax (jquery) to send your data to server.
JS
$.ajax({url:"yourfile.php",type:"POST",async:false,
data:{getlat:$("#getlat").val(),getlon:$("#getlon").val()}
});
PHP
$lat = $_POST["getlat"];
$lon = $_POST["getlon"];
...
Another solution is to use cookies. Personally i use this to get/set my cookies:
JS
....
$.cookie("data",{getlat:$("#getlat").val(),getlon:$("#getlon").val()});
PHP
$data = json_decode($_COOKIE["data"]);
$lat = $data["getlat"];
$lon = $data["getlon"];
...