在javascript中访问php变量

时间:2011-12-20 07:31:06

标签: php javascript geolocation

我想要实现的目标基本上是标记登录我聊天的所有用户的位置,这是另一回事。所以我虽然使用geolocation api(http://code.google.com/apis/gears/api_geolocation.html)并将生成的位置存储在会话变量中。但它不起作用。这是代码 - “

<?php 
    $my_lat= $_GET['test'];             // get data
    $my_long= $_GET['fname']; 
    $my_name = "vivek";
    $lat1=28.635308;
    $long1=77.22496;
    //$latitude = $_GET['latitude'];
    //$longitude = $_GET['longitude'];// get data
    echo $my_lat.''.$my_long;
    ?>

    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=''
&sensor=true"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        if(navigator.geolocation) {

          navigator.geolocation.getCurrentPosition(function(position) {
            var lat  = position.coords.latitude;
            var longi= position.coords.longitude;
            var  url = 'bam.php';
        //post to the server!
        $.get(url, { test: lat,fname: longi },function(data){
        alert('data was passed!');
        });


          }, function() {
            //now is when the marking should be done.
            handleNoGeolocation(true);
          });

  }
    });
</script>

<script type="text/javascript">
  function initialize() {
    var myLatlng = new google.maps.LatLng(28.635308,77.22496);
    var myOptions = {
      zoom: 4,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    var thelat = '<?php echo $my_lat;?>';
    var thelong = '<?php echo $my_long;?>';
    var seslatlng = new google.maps.LatLng(thelat,thelong);
    alert(seslatlng);
    var marker = new google.maps.Marker({
        position: seslatlng, 
        map: map,
        animation: google.maps.Animation.DROP,
        title:"Hello world!",
    }); 



}

</script>`  

问题是。变量seslatlng设置为(0,0)始终不是我想要的。我是PHP的新手,我只是粗略地了解服务器端脚本如何工作。有人可以解决这个问题吗?

4 个答案:

答案 0 :(得分:2)

不使用 将值分配给javascript变量,而是使用:

var thelat = '<?=$my_lat?>';
var thelong = '<?=$my_long?>';

它会对你有用。我也尝试过将php变量赋值给javascript变量。

答案 1 :(得分:1)

我没有看到你设置seslatlng的任何地方。如果您希望将其作为JS变量,请添加:

var seslatlng = "(<?php echo $my_lat . ',' . $my_lng; ?>)"; // (30.230, 8.030)

答案 2 :(得分:1)

当你使用jquery的ajax get函数时,你想要做的是调用返回位置的php函数。这应该是怎么看的:

<?php 

    if ( $_GET['test'] ) {
        $my_lat= $_GET['test'];             // get data
        $my_long= $_GET['fname']; 
        $my_name = "vivek";
        $lat1=28.635308;
        $long1=77.22496;
        //$latitude = $_GET['latitude'];
        //$longitude = $_GET['longitude'];// get data
        echo $my_lat.''.$my_long;
    } else {
    ?>

    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=''
&sensor=true"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        if(navigator.geolocation) {

          navigator.geolocation.getCurrentPosition(function(position) {
            var lat  = position.coords.latitude;
            var longi= position.coords.longitude;
            var  url = 'bam.php';
        //post to the server!
        $.get(url, { test: lat,fname: longi },function(data){
        alert('My location is: ' + data);
        });


          }, function() {
            //now is when the marking should be done.
            handleNoGeolocation(true);
          });

  }
    });
</script>

<script type="text/javascript">
  function initialize() {
    var myLatlng = new google.maps.LatLng(28.635308,77.22496);
    var myOptions = {
      zoom: 4,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    var thelat = '<?php echo $my_lat;?>';
    var thelong = '<?php echo $my_long;?>';
    var seslatlng = new google.maps.LatLng(thelat,thelong);
    alert(seslatlng);
    var marker = new google.maps.Marker({
        position: seslatlng, 
        map: map,
        animation: google.maps.Animation.DROP,
        title:"Hello world!"
    }); 



}

</script>
<?php } ?>

答案 3 :(得分:0)

最好的方法是:

<script type="text/javascript">
    var something=<?php echo json_encode($a); ?>;
</script>

json_encode的目的是转义引号,其他实体<?php echo $a; ?>可能会破坏您的代码。