1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

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:
    SEMrush
    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
    SEMrush
  2. SpacePhoenix

    SpacePhoenix Well-Known Member

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

    pesst Well-Known Member

    Messages:
    466
    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:
    5,587
    Likes Received:
    1,041
    Best Answers:
    19
    Trophy Points:
    400
    #4
    qwikad.com, Oct 14, 2018 IP