I am using the following code to find the browser location and post it to the next page, but the issue is JavaScript is taking little time to find the browser location and form is posting before the browser location is found, how can I make the form submit only after finding the browser location?
newtest1.php
<html>
<head>
<script>
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
}
}
function showPosition(position) {
document.getElementById("getlat").value = position.coords.latitude;
document.getElementById("getlon").value = position.coords.longitude;
}
getLocation();
</script>
</head>
<body>
<form id="myForm" method="post" action="newtest2.php">
<input type="text" id="getlat" name="getlat" />
<input type="text" id="getlon" name="getlon" />
<input type="button" name="myformer" onclick="myFunction();" />
</form>
<script>
function myFunction() {
document.getElementById("myForm").submit();
}
myFunction();
</script>
</body>
</html>
newtest2.php
<?php
echo $_POST['getlat'];
echo $_POST['getlon'];
?>