0

I would like to ask, how do i make a PHP session variable to be equal to a Javascript value?

What I am trying to achieve is this:

$_SESSION['lat'] = Javascript geolocation latitude value;
$_SESSION['lng'] = Javascript geolocation longitude value;

The JS geolocation function I currently have is:

<script>
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else{x.innerHTML="Geolocation is not supported by this browser.";}
}
function showPosition(position)
{   
document.getElementById('inputfield3').value = position.coords.latitude;
document.getElementById('inputfield4').value = position.coords.longitude;
}
</script>

How do I use getLocation() such that the $_SESSION['lat'] and $_SESSION['lng'] are equals to the latitude and longitude?

As I would need $_SESSION['lat'] and $_SESSION['lng'] to do a SELECT in my database. Thank you.

user2192094
  • 287
  • 1
  • 5
  • 13

3 Answers3

1

As we know from basic understanding of these two languages;

  • PHP is server side
  • JavaScript is client-side.

We also know that we can invoke requests from the client to the server via AJAX calls. I'd suggest looking into that to solve your problem.

ʰᵈˑ
  • 10,738
  • 2
  • 20
  • 45
1

You can use Ajax for that:

var httpRequest = null;
function getHttpObject()
{
    if (httpRequest != null)
        return;

    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        httpRequest = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE 8 and older
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }
}

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

function showPosition(position)
{   
    document.getElementById('inputfield3').value = position.coords.latitude;
    document.getElementById('inputfield4').value = position.coords.longitude;

    getHttpObject();

    // I don't know how to get the positions for showPosition, so you may have to change this part.
    httpRequest.open("GET", "set_position.php?lat=" + position.coords.latitude + "&lon=" + position.coords.longitude, true);

    httpRequest.onreadystatechange = function()
    {
        if (httpRequest.readyState == 4 && httpRequest.status == 200)
        {
            // Handle the response here. Show error or success message or whatever...
        }
    };

    httpRequest.send(null);
}

Then on your php script you can set the values to the session, verifying it and verifying the values(of course).

Note: It's been a century I don't write ajax in pure JS. Please warn me for errors.

UPDATE:

I have fixed the way of using the callback of getCurrentPosition according to this and this documentation.

UPDATE 2:

You can use the common way to send that data to database with a form. Forget everything above and try this:

Suposing you have a form like this:

<form method="post" action="set_positions.php" id="form1">
    <input type="text" id="inputField3" />
    <input type="text" id="inputField4" />
</form>

Try this JS code:

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

function showPosition(position)
{   
    document.getElementById('inputfield3').value = position.coords.latitude;
    document.getElementById('inputfield4').value = position.coords.longitude;
    document.getElementById('form1').submit();
}

So you'll receive those fields in your PHP script. Note that those fields can be hidden fields too if you doesn't want to user to change its value.

DontVoteMeDown
  • 19,660
  • 10
  • 65
  • 96
  • Thanks DontVoteMeDown, I will look into it. – user2192094 Dec 27 '13 at 12:44
  • How do I get real time geolocation latitude and longitude, so that I can use it to SELECT from my PHP database? – user2192094 Dec 27 '13 at 13:38
  • @user2192094 I have updated my answer. – DontVoteMeDown Dec 27 '13 at 13:45
  • thanks DontVoteMeDown, but i dun really understand how does it works? Do you mind explaining? – user2192094 Dec 27 '13 at 13:57
  • @user2192094 you'll call `getLocation()` that will try to get the position from your device. If succeeded, it will call the success callback - the first parameter of the function - that stands for `showPosition()`. It's first parameter is the position object. After setting the value of the fields, it will open an ajax call sending the positions to the server side script. You php script will receive `$_GET["lat"]` and `$_GET["lon"]`. Then you only have to put those variables in the session. – DontVoteMeDown Dec 27 '13 at 14:01
  • DontVoteMeDown, thanks for your explanation. Let me try to understand. – user2192094 Dec 27 '13 at 14:39
  • @user2192094 If all this' getting complicated, you can do something simpler, like send a `form` with those `inputField3` and `inputField4` to php. Its the common way. See my secund update. – DontVoteMeDown Dec 27 '13 at 15:53
  • DontVoteMeDown, thanks for taking your time to explain. I have tried your Update 2 method. It is now working. Thanks! – user2192094 Dec 28 '13 at 07:57
  • @user2192094 glad to help! You're welcome. – DontVoteMeDown Dec 28 '13 at 18:46
1

May be this will help you,

you can use COOKIE with JAVASCRIPT...

See below functions...

function setCookie(cname, cvalue, exhour)
{
    var d = new Date();
    d.setTime(d.getTime()+(exhour*60*60*1000));
    var expires = "expires="+d.toGMTString();
    document.cookie = cname + "=" + cvalue + "; " + "path=/" + "; " + expires;
}
function getCookie(cname)
{
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) 
    {
        var c = ca[i].trim();
        if (c.indexOf(name)==0) return c.substring(name.length,c.length);
    }
    return "";
}

And you can get COOKIE information in PHP(server side) using $_COOKIE...

Akshay Paghdar
  • 3,439
  • 2
  • 17
  • 34