1

This code works perfectly in the fact that it gets my longitude and latitude via javascript, and outputs to the screen. But I want to pass these values to a PHP variable, here is what I currently have. I'm not a javascript expert, although I have spent 5 to 6 hours researching and trying different things before I ask Stackoverflow. Thanks for any help.

    <!DOCTYPE html>
    <html>
    <body>

    <p id="long"></p>
    <p id="lat"></p>

    <script>
    var x = document.getElementById("long");
    var y = document.getElementById("lat");

    if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
    } else { 
    x.innerHTML = "Geolocation is not supported by this browser.";
    }

    function showPosition(position) {

    x.innerHTML = position.coords.longitude;  //displays longitude on screen
    y.innerHTML = position.coords.latitude;  //displays latitude on screen

    }

    </script>

    <?php 

    echo $plong = '<script>x.innerHTML = position.coords.longitude;</script>';
    echo $plat = '<script>y.innerHTML = position.coords.latitude;</script>';

    echo "long " . $plong;
    echo "lat " . $plat;

    ?>

    </body>
    </html>
Ricky T
  • 243
  • 1
  • 13
  • Is the file saved with extension .html/.htm or .php ? Also I suggest you this: http://stackoverflow.com/questions/25539213/passing-values-from-javascript-to-php-using-ajax – Luigi Cerone Sep 02 '16 at 17:17
  • If you want to save the values into a table or something send them via AJAX, you will find a lot of info here in Stackoverflow about how to achieve this – Professor Zoom Sep 02 '16 at 17:19
  • saved with .php extension – Ricky T Sep 02 '16 at 17:20
  • If at all possible, I want to keep it as simple as stated above. This is just a snipit of my PHP page. The rest of my page is just waiting for these two values, and i'll be set. – Ricky T Sep 02 '16 at 17:26

0 Answers0