I followed the w3schools tutorial to extract the GPS location: https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_geolocation_error modified to start when the website loads: <html> <body onload="getLocation()"> <p hidden id="demo"></p> <form> <input type="hidden" id="GPS" name="GPS" value="HERE"> <input type="submit" value="Submit"> </form> <script> var x = document.getElementById("demo"); function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition, showError); } else { x.innerHTML = "Geolocation is not supported by this browser."; } } function showPosition(position) { x.innerHTML = position.coords.latitude + " , " + position.coords.longitude; } function showError(error) { switch(error.code) { case error.PERMISSION_DENIED: x.innerHTML = "User denied the request for Geolocation." break; case error.POSITION_UNAVAILABLE: x.innerHTML = "Location information is unavailable." break; case error.TIMEOUT: x.innerHTML = "The request to get user location timed out." break; case error.UNKNOWN_ERROR: x.innerHTML = "An unknown error occurred." break; } } </script> </body> </html> Code (markup): The script works good showing latitude and longitude in the <p>: <p id="demo"></p> Code (markup): What I want to do is including that latitude and longitude in the hidden input, more precisely in the value you see "HERE", instead of showing in the <p> I spent so many hours but I couldn't find a solution! TIA Thanks in advance
it was fixed, solution: <html> <body onload="getLocation()"> <p id="demo"></p> <form> <input type="hidden" id="GPS" name="GPS" value="HERE"> <input type="submit" value="Submit"> </form> <script> var x = document.getElementById("demo"); function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition, showError); } else { x.innerHTML = "Geolocation is not supported by this browser."; } } function showPosition(position) { x.innerHTML = position.coords.latitude + " , " + position.coords.longitude; document.getElementById('GPS').value = position.coords.latitude + " , " + position.coords.longitude; } function showError(error) { switch(error.code) { case error.PERMISSION_DENIED: x.innerHTML = "User denied the request for Geolocation." break; case error.POSITION_UNAVAILABLE: x.innerHTML = "Location information is unavailable." break; case error.TIMEOUT: x.innerHTML = "The request to get user location timed out." break; case error.UNKNOWN_ERROR: x.innerHTML = "An unknown error occurred." break; } } </script> </body> </html> Code (markup):