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.

2
  • 1
    You can't do this, javascript is client side, php is server side. A way can be to use an ajax call Commented Dec 27, 2013 at 11:55
  • Thanks, Alessandro Minoccheri and Alex. I will look into it. Commented Dec 27, 2013 at 12:42

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks DontVoteMeDown, I will look into it.
How do I get real time geolocation latitude and longitude, so that I can use it to SELECT from my PHP database?
thanks DontVoteMeDown, but i dun really understand how does it works? Do you mind explaining?
@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, thanks for your explanation. Let me try to understand.
|
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.

1 Comment

Thanks, Harry Denley, I will look into it.
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...

1 Comment

Thanks, Akshay Paghdar, I will look into it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.