1

Possible Duplicate:
Access a JavaScript variable from PHP

I have one JS code that return my geolocation (latitude, longitude) and I would like to reuse these information in a PHP code. My webpage has .php extention and I have set global variable but it doesn't work. How can I do that ?

<script type="text/javascript"> 
    function getCoordPosition(){
        if(navigator.geolocation){
            navigator.geolocation.getCurrentPosition(function(position){
                latitude = position.coords.latitude;
                longitude = position.coords.longitude;
            });
        }
    }
</script>

<?php
    echo 'Latitude is $latitude';
?>
Community
  • 1
  • 1
  • possible duplicate of [Access a JavaScript variable from PHP](http://stackoverflow.com/questions/2338942/access-a-javascript-variable-from-php). See also http://stackoverflow.com/questions/7016701/creating-jquery-ajax-requests-to-a-php-function/7016795#7016795 – JJJ Mar 02 '12 at 18:19
  • The only solution to this is to use AJAX calls - this is the only way how JS will communicate with PHP. – MacMac Mar 02 '12 at 18:19
  • @BurningtheCodeigniter: No, there are plenty of other solutions. Communication is usually bidirectional, so sending data from PHP to JS is as simple as generating JS code using PHP. In the other direction you have other solutions to send data from JS to PHP, such as window reload with modified GET parameters, adding image/frame/script pointing to PHP script etc. Saying that "_AJAX calls - this is the only way how JS will communicate with PHP_" is completely incorrect. – Tadeck Mar 02 '12 at 19:40

5 Answers5

2

You can't just combine the two; Javascript executes in the browser, PHP on the server (before the page even reaches the browser). If you want to communicate a Javascript result to your server, your should submit it using a form, or use AJAX for asynchronous requests.

Another Code
  • 3,083
  • 21
  • 23
1

One of the methods are from $_GET method

var test1 = "myvalue";
var link = "thispage.php";

window.location = link+"?test1="+test1;

Then read the value from the testpage.php

Another methods is $.get, or $.post request to a php page.

$.post("mypage.php", {
   myvar : "myvalue",
   "myanothervar" : "myanothervalue"
}, function(data) {
   //data contains the output from the script
});
Starx
  • 77,474
  • 47
  • 185
  • 261
1

I think the issue is that you are mixing client side code (javascript) with server side code (php). The php on the server cannot access the information from the client at run time since the client runtime doesn't exist yet. The client must submit it back to the server for use.

The php code executes and the response is sent to the client. Then the client page is executed from the top of the page to the bottom. At that point the server doesn't know what's going on on the client. You could load the client page and and then use jQuery to make a server side call (AJAX) back to php to pass it the information.

something like (I'm a bit rusty on jQuery)

$.get('myurl/sendgeolocation', { lat : lattitude, long : longitude }, function (data) {});

Then you would want the server do do whatever it needs to do in the function(data) callback to update the screen as necessary or whatever.

Rob Jefferies
  • 254
  • 3
  • 9
0

Your PHP code is processed first by the server and then sent to the viewer. Then the JavaScript code is executed, so PHP has no way to know what JavaScript is doing since it is processed prior to the JavaScript. You could use AJAX to pass the JavaScript information to a PHP script and then get something back from it, but it really depends on what you want to do with all this.

j08691
  • 204,283
  • 31
  • 260
  • 272
  • I want to send these data to my server (with user name and other things) and store them in a database. I'll try one AJAX call to do that. Tx to clarify JS and PHP particularity. –  Mar 02 '12 at 18:42
0

This is what exactly the answer you expect

//index.php

<html>
<head>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript"> 
        function getCoordPosition()
        {
                if(navigator.geolocation)
                {
                navigator.geolocation.getCurrentPosition(function(position){
                                                                    latitude = position.coords.latitude;
                                                                    longitude = position.coords.longitude;
                                                                    obj ={};
                                                                    obj.latitude = position.coords.latitude;
                                                                    obj.longitude= position.coords.longitude;
                                                                    //latlngvalue = $.get('index.php',obj,"html");
                                                                    $.ajax({
                                                                       url:'fetch.php',
                                                                       data:obj,
                                                                       dataType:'html',
                                                                       success:function(obj){
                                                                           latlng = obj.split('-');

                                                                           $('#latlng').html("Latitude : "+latlng[0]+"| Longitude :"+latlng[1]);
                                                                       }
                                                                    });

                                                                    });
                }
        }
</script>
</head>
<body>
<div id="latlng"></div>
<button onclick="getCoordPosition()" >Get Geocode</button>
</body>
</html>

//fetch.php

<?php
if(isset($_GET['latitude']) && isset($_GET['longitude'])):
    echo $_GET['latitude'].'-'.$_GET['longitude'];
endif;

?>
Sam Arul Raj T
  • 1,752
  • 17
  • 23