HTML5 geolocation, pass lat/long to PHP variable when page load

Adarsh v s

in this code i need to assign lat and long as a php variable .. pls help me

<!DOCTYPE html><html><body><p id="demo"></p><script>
    var x=document.getElementById("demo");
    function getLocation()
     {
    if (navigator.geolocation)
    {
    navigator.geolocation.getCurrentPosition(showPosition);
    }
    else{x.innerHTML="Geolocation is not supported by this browser.";}
    }
    function showPosition(position)
    {
    x.innerHTML="Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude;  
    }
    getLocation()
    </script>

MackProgramsAlot

This assumes that you have a 'users' table with long and lat columns.

somescript.php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$stmt = $conn->prepare("INSERT INTO users (long, lat) VALUES (:long, :lat)");
$stmt->bindParam(':long', $long);
$stmt->bindParam(':lat', $lat);
$long = $_REQUEST['long'];
$lat = $_REQUEST['lat'];
$stmt->execute();

Your javascript:

function getLocation()
{
    if (navigator.geolocation)
    {
        navigator.geolocation.getCurrentPosition(showPosition);
    }
}
function showPosition(position)
{
    var lat = position.coords.latitude;
    var long = position.coords.longitude;
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
            alert("Success");
        }
    };
    xmlhttp.open("GET", "somescript.php?lat=" + lat + "&long=" + long, true);
    xmlhttp.send();
}
getLocation();

similarly you would do the same for an UPDATE statement.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

HTML5 geolocation, pass lat/long to PHP variable on click

From Dev

HTML5 refresh page on geolocation change

From Dev

pass variable between php and javascript without load page

From Dev

PHP update variable on page load

From Dev

Geolocation prompt before page load?

From Dev

Pass the variable to next page PHP

From Dev

Pass PHP variable from page to class to page

From Dev

HTML5 Load page from offline cache only when not online

From Dev

Html5 application cache: Unable to load JavaScript file from application cache when refresh page

From Dev

Pass variable PHP/HTML

From Dev

jquery load pass variable to php file

From Dev

PHP Static variable reloads after Page Load

From Dev

Pass jquery - generated variable to php in same page

From Dev

Pass variable in URL to another PHP page

From Dev

pass a variable to php page via dropzone url?

From Dev

Pass PHP fetch variable to another page

From Dev

HTML5 geolocation: How to get pass lat and long values to an AJAX call after a success or error callback

From Dev

HTML5/Javascript GeoLocation: Pass data to callback function -or- suspend async call (with promises)?

From Dev

How to pass and put variable from a php page into a another page(form)?

From Dev

HTML5 geolocation is not accurate

From Dev

HTML5 geolocation is not accurate

From Dev

Pass variable from PHP into HTML

From Dev

JSP pass hidden input value to servlet when the page load

From Dev

Php Variable is null when i pass it to the array

From Dev

HTML5 Video to play after AJAX page load

From Dev

access vba bombs when defining html results page as variable to extract content from php page

From Dev

load() inserts the entire page instead of a fragment when using variable

From Dev

How to store drop down selected value in variable on page load in php?

From Dev

External resources fail to load when using HTML5 cache

Related Related

  1. 1

    HTML5 geolocation, pass lat/long to PHP variable on click

  2. 2

    HTML5 refresh page on geolocation change

  3. 3

    pass variable between php and javascript without load page

  4. 4

    PHP update variable on page load

  5. 5

    Geolocation prompt before page load?

  6. 6

    Pass the variable to next page PHP

  7. 7

    Pass PHP variable from page to class to page

  8. 8

    HTML5 Load page from offline cache only when not online

  9. 9

    Html5 application cache: Unable to load JavaScript file from application cache when refresh page

  10. 10

    Pass variable PHP/HTML

  11. 11

    jquery load pass variable to php file

  12. 12

    PHP Static variable reloads after Page Load

  13. 13

    Pass jquery - generated variable to php in same page

  14. 14

    Pass variable in URL to another PHP page

  15. 15

    pass a variable to php page via dropzone url?

  16. 16

    Pass PHP fetch variable to another page

  17. 17

    HTML5 geolocation: How to get pass lat and long values to an AJAX call after a success or error callback

  18. 18

    HTML5/Javascript GeoLocation: Pass data to callback function -or- suspend async call (with promises)?

  19. 19

    How to pass and put variable from a php page into a another page(form)?

  20. 20

    HTML5 geolocation is not accurate

  21. 21

    HTML5 geolocation is not accurate

  22. 22

    Pass variable from PHP into HTML

  23. 23

    JSP pass hidden input value to servlet when the page load

  24. 24

    Php Variable is null when i pass it to the array

  25. 25

    HTML5 Video to play after AJAX page load

  26. 26

    access vba bombs when defining html results page as variable to extract content from php page

  27. 27

    load() inserts the entire page instead of a fragment when using variable

  28. 28

    How to store drop down selected value in variable on page load in php?

  29. 29

    External resources fail to load when using HTML5 cache

HotTag

Archive