latitude / longitude data

save latitude and longitude data from a form to a database.

Overview

The issue occurs because the JavaScript code attempts to access the position data outside the asynchronous callback function.
Ensure the latitude and longitude values are assigned to hidden form fields within the correct callback function, then submit the form to store the data in the database.

Answered
ChronoForms v7
da daddyp 09 Oct, 2023
Hello, i must save latitude and longitude data on db.
I obtain the data from the script, but i'm not able to save data on db.

<!DOCTYPE html>
<html>
<body>

<body onload="getLocation()">
<p id="demo"></p>

<script>
var x = document.getElementById("demo");

function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
    document.getElementsByName('lat')[0].value = position.coords.latitude;
    document.getElementsByName('lng')[0].value = position.coords.longtitude;
  } else { 
    x.innerHTML = "Geolocation is not supported by this browser.";
  }
}
    
function showPosition(position) {
  x.innerHTML = "Latitude: " + position.coords.latitude + 
  "<br>Longitude: " + position.coords.longitude +
  "<br>Precisione: " + position.coords.accuracy;
}
</script>

</body>
</html>


Anyone can help me?

Thanks
Max_admin Max_admin 11 Oct, 2023
Answer
if you are able to set the value of 2 hidden form fields to lat and lang then save the form then it should be stored.

you can check if the fields are set using the browser's console
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
This topic is locked and no more replies can be posted.