I have the following Javascript code:
<script>
function getLocation(){
var lat;
var lon;
navigator.geolocation.getCurrentPosition((pos)=>{
lat = document.getElementById("lat").value=pos.coords.latitude;
lon = document.getElementById("lon").value=pos.coords.longitude;
});
}
getLocation();
</script>
With this code I get the latitude and longitude of the user, but I would like to include it in the values of the hidden fields of the following form:
<form method="post">
<input type="submit" name="BtnLocalizacao" value="Minha Localização" onclick="getLocation()">
</form>
To retrieve the values with POST.
<?php if($_POST["BtnLocalizacao"] == "Minha Localização"){ ?>
<form method="post">
<input type="hidden" name="Latitude" id="lat">
<input type="hidden" name="Longitude" id="lon">
<input type="submit" name="BtnSalvar" value="Salvar">
</form>
<?php } ?>
But I can not. How can I pass JavaScript values, pass to the hidden field, and retrieve them with the post?
Do the following: put var lat = document.getElementById("lat") on one line, and on the next: lat.value = latitude