Need help to send data to a hidden field

Discussion in 'Programming' started by pesst, Oct 12, 2018.

  1. #1
    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
     
    pesst, Oct 12, 2018 IP
  2. SpacePhoenix

    SpacePhoenix Well-Known Member

    Messages:
    197
    Likes Received:
    28
    Best Answers:
    2
    Trophy Points:
    155
    #2
    What server-side language is in use?
     
    SpacePhoenix, Oct 12, 2018 IP
  3. pesst

    pesst Well-Known Member

    Messages:
    471
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    110
    #3
    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):
     
    pesst, Oct 12, 2018 IP
  4. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,278
    Likes Received:
    1,696
    Best Answers:
    31
    Trophy Points:
    475
    #4
    qwikad.com, Oct 14, 2018 IP