0

So I can get it to print out to page using x.innerHTML.
But how can I turn it into Variables to use however I wish with echo and print in PHP???? I need it to Operate in 2 fashions

  1. When the page loads it ask for permission then takes the lat and long and just stores it in memory which ill use session keys to pass the data through the pages and fill out hidden form fields or email to other users depending on what I need to do and how I need it to operate.
    2.Once a button is pressed it ask for permissions then saves the data in the temp session

I tried

 $long2  = position.coords.longitude;

  $lat2  = position.coords.latitude;

I want to be able to use this at will in my php pages

   echo  $lat2;
   echo "<br>";
   echo $long2;`

<?php
session_start();
$token= md5(uniqid());
$_SESSION['delete_customer_token']= $token;
session_write_close();
?>

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) {
  x.innerHTML = "Latitude: " + position.coords.latitude + 
  "<br>Longitude: " + position.coords.longitude;
}
<!DOCTYPE html>
<html>
<body>

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>



echo "<a href='https://maps.google.com/?q=$lat2,$long2'>Get Directions</a>";




</body>
</html>
  • You don't have access to JavaScript in your PHP code. That only runs after it arrives on the client and executes. You will need to create a POST controller on your PHP server that accepts the data - then, POST the coordinates to that PHP endpoint. – Randy Casburn Dec 23 '20 at 16:24
  • so a user fills out a form. Then presses submit button. it ask for permission, post the data to the form field. then emails it as a variable? – qwertyyuiop Dec 23 '20 at 16:32
  • What @RandyCasburn wrote is one way to do it. You could also post the location data after requesting it directly via XHR (AJAX). It is not important if you use POST or GET. You could call your PHP endpoint via Fetch https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch (AJAX) and send the coordinates as GET parameters. The session has to be already active for this to work. – jmartsch Dec 23 '20 at 16:47
  • There are lots of ways to send data from a webpages. See https://stackoverflow.com/questions/1968296/how-to-i-send-data-from-javascript-to-php-and-vice-versa – FluxCoder Dec 24 '20 at 01:32

0 Answers0