dongzhidian3538 2015-06-20 02:30
浏览 55
已采纳

从javascript调用php

I have an index.php which contains both JS and PHP code. The PHP code reads GPS coordinates from an iOS app and the JS reads these coordinates back to be displayed on a web app.

I am stuck on figuring out how to call the PHP every few seconds so I can get the new coordinates. I am clueless as to how to approach this and it seems like a recursion if I need to call the same file for that purpose.

what is the best way to "refresh" the php code and read the new coordinates? Here is my code:

<?php include_once('location.php') ?>

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>My GeoLocation</title>
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>


    <script>

var map;
google.maps.visualRefresh = true;  

function initialize() {
  var myLatlng = new google.maps.LatLng(<?php echo $current_lat ?>, <?php echo $current_long ?>);
  var mapOptions = {
    zoom: 14,
    center: myLatlng
  };

  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

  var marker = new google.maps.Marker({
      position: myLatlng,
      map: map,
      title: 'Hello World!'
  });

  yourFunction();
}


function yourFunction(){

alert ("<?php echo $current_lat ?>, <?php echo $current_long ?>"); --> here I get the same coordinates 

//Will add code here to display the new lat/long once I figure out how to refresh them 

    setTimeout(yourFunction, 1000);
}

google.maps.event.addDomListener(window, 'load', initialize);

    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>

$content = file_get_contents('php://input');
$post_data = json_decode($content , true);
$lat = $post_data['lat'];
$long = $post_data['long'];

//CONNECT TO MYSQL
$con1 = mysql_connect("localhost", "aaaaaa", "bbbbbb", "location111");
if ($con1->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$db_selected = mysql_select_db('location111');
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}

if (!empty($lat)) {
  $sql = "INSERT INTO LocationInfo (latitude, longitude) 
                VALUES ('$lat', '$long');";
  mysql_query($sql) or die ('Error updating database: ' . mysql_error());
 }

$read_query = "SELECT * FROM LocationInfo;";
$results = mysql_query($read_query) or die ('Error reading from database: ' . mysql_error());
$column = array();

while($row = mysql_fetch_array($results)){
    $column[] = $row;
}

$current_lat = $column[sizeof($column) - 1]['latitude'];
$current_long = $column[sizeof($column) - 1]['longitude'];


?>
  • 写回答

1条回答 默认 最新

  • dream_high1026 2015-06-20 02:36
    关注

    You're probably looking to implement AJAX.

    What you would do is separate the PHP code from your Javascript, and make a page that simply outputs the coordinates.

    You then call this page from your Javascript, and you'll get the result in a variable.

    The easiest (but not best as you'll eventually come to realise) way to implement this is using jQuery: http://api.jquery.com/jquery.ajax/

    $.ajax("mypageurlhere", {
      success: function(data) {
        console.log("Data: " + data);
      },
      error: function() {
        console.log("Error loading data");
      }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器