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 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错