Dean Elliott Dean Elliott - 5 months ago 34
jQuery Question

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

Is it possible to get the latitude / longitude of a user using HTML5 geolocation when they click a button and then redirect to a new page where the values will have been passed to PHP variables?

So if a user is on

, which contains the HTML5 geolocation code, and they click on this button;

<span class="button get-my-location">My Location</span>

They'll then be redirected to
where their lat/long will be stored in 2 PHP variables

Is this possible? I assume it would need to be AJAX based? Unfortunately AJAX isn't my strong suit.

Any help would be great


It's easy to get longitude and latitude for user and you will need to assign it to hidden field in form and submit it to the php page url or send it as GET or POST by Javascript

This is example how to redirect user directly after having his location:

<!DOCTYPE html>

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

var x = document.getElementById("demo");

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

function redirectToPosition(position) {





//do whatever you want