Loading PHP variable with JS variable values

I have a Blank application that uses js to get the browser long/lat .
I need to load the js variables into php variables inside the same blank app.
Please can someone advise how I can get
$latX = position.coords.latitude;
$lngY = position.coords.longitude;

Below is my actual html and js code,

var x = document.getElementById(“location”);
var lng = “”;
var lat = “”;
getLocation();

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

function showPosition(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
var timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
}

You cannot directly transfert JS variables to PHP because the JS is running on the client device and PHP on your server.

You need to send the information to the server. You can do that with an Ajax call that will look like this

$.ajax({
    url: '../Blank_App/index.php',
	type: 'POST',
	data: 'Lat=' + position.coords.latitude + '&Long=' + position.coords.longitude,
	success: function(data){
	},
	error: function(data) {
	     alert("Trouble");
	}
});

You also need to modify your blank app to process the Lat and Long data that it will receive

Thanks but I just can’t get things to work…aaaaarg (so frustrated).
The app that runs the browser location checking is called BrowserCheck. (the code is further down).
I then wrote the receiving blank app (called LocationUpdate the code is also further down) that receives the data (It works when you call it from your browser).

The primary job of the apps is to get the browsers lat/lng and consistently call LocationUpdate (thus updating the users lat/lng. I know my loop is working as I can see it looping through and showing the results using x.innerHTML.

To send the lat and lng I have tried with no luck:
var xhttp = new XMLHttpRequest();
xhttp.open(“POST”, “https://www.xxxxx/LocationUpdate/LocationUpdate.php?deviceID=” + deviceID + “&lng=” + lng + “&lat=” + lat, true);
xhttp.send();

and your ajax.

The only thing that seems to work is, which is crazy (why open another window)

let url = “https://www.xxxxxxx/LocationUpdate/LocationUpdate.php?deviceID=” + deviceID + “&lng=” + lng + “&lat=” + lat ;
window.open(url,’_blank’);

BrowserCheck App:
$lat=’’;
$lng=’’;
$deviceID=[deviceID];

The script part starts here
var x = document.getElementById(“location”);
var lng = “”;
var lat = “”;
var deviceID = “”;
var $timezone = “”;
function timedRefresh(timeoutPeriod) {
setTimeout(“location.reload(true);”,timeoutPeriod);
}

window.onload = timedRefresh(10000);
setInterval(function() {
getLocation();
} , 10);

function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);

} else {
x.innerHTML = “Geolocation is not supported by this browser.”;
}
}
function showPosition(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
var deviceID = <?php echo json_encode($_SESSION['deviceID']); ?>;
var timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
// var xhttp = new XMLHttpRequest();
// xhttp.open(“POST”, “https://www.xxxxxxxx/LocationUpdate/LocationUpdate.php?deviceID=” + deviceID + “&lng=” + lng + “&lat=” + lat, true);

// xhttp.send();

// let url = “https://www.xxxxx/LocationUpdate/LocationUpdate.php?deviceID=” + deviceID + “&lng=” + lng + “&lat=” + lat;

// window.open(url,’_blank’);
x.innerHTML = "Latitude: " + lat + " Longitude: " + lng + " DeviceID: "+ deviceID;
}

function errorHandler(err) {
if(err.code == 1) {
alert(“Error: Please ENABLE GPS on your device!”);
}

else if( err.code == 2) {
// alert(“Error: Position is unavailable!”);
}
}

LocationUpdate app (code below):

$lat=[lat];
$lng=[lng];
$deviceID=[deviceID];
if($lat <> “0”){
$update=date(“Y-m-d h:i:s”);
$update_table = ‘IsawDevices’;
$update_where = “deviceID = ‘$deviceID’”;
$update_fields = array(
“lat = ‘$lat’”,
“lng = ‘$lng’”,
);
$update_sql = ‘UPDATE ’ . $update_table
. ’ SET ’ . implode(’, ', $update_fields)
. ’ WHERE ’ . $update_where;
sc_exec_sql($update_sql);
sc_commit_trans();
// echo “Updated: lat=”.$lat." lng=".$lng;
}
else
{
// echo “No Update”;
}

//sc_exit();

Your refresh is very fast, 10000 = 10 seconds, if you have many user it can slow down your server

Yes, just needed that for testing

Hello Larryh1115,
interesting project. Would you kindly share the complete code for both applications for a reverse engineering exercise ?

and do you uncomment out the lines with // in front of them ?

Thanks in advance