I am asking for user's location on page loading and set it to hidden fields and try to read it from their to php. The JS code works but when I echo it to php, its showing null and after printing 'value is submitted' it keeps reloading the whole page.
<script>
var x = document.getElementById(\"latitude\");
var y = document.getElementById(\"longitude\");
var a;
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
} else
{
x.innerHTML = \"Geolocation is not supported by this browser.\";
}
}
function showPosition(position)
{
x.value = position.coords.latitude;
y.value= position.coords.longitude;
submitform();
}
function submitform()
{
document.forms[\"locform\"].submit();
console.log(\"Value is sumitted\");
}
getLocation();
</script>
<form action='abc.com/list' id='locform' method='get'>
<input type='hidden' id='latitude' name='latitude'>
<input type='hidden' id='longitude' name='longitude'>
</form>
$users_postal=$_GET['latitude'];
echo($users_postal);