使用php

时间:2015-11-22 05:38:58

标签: javascript php mysql xmlhttprequest

我试图通过HTML5地理位置获取用户的位置。凭借我获得的纬度和经度,我想使用php查询数据库,以获得25英里内最近的位置。 javascript我的工作是获取用户位置以及打印纬度和经度,但是当我调用我的php文件来获取它的近似位置时,它似乎没有工作。

编辑2: 似乎getClosest()未被调用,因为样本打印输出甚至无法打印到我的<p id="r"> < /强>

编辑:我编辑了我的PHP代码,所以当我直接进入php链接时它现在可以工作但是它似乎没有从javascript获取信息,因为它&#39;没有打印到第一页。

提前致谢。这是我的html / javascript:

    <button type="button" onclick="getLocation()">Try It</button>

  <p id="demo"></p>
<div id="txtHint"><b>Person info will be listed here...</b></div>
<p id="r">...</p>

  <script>
  var x = document.getElementById("demo");
  var txt = document.getElementById("txtHint");
  var r = document.getElementById("r");

  function getLocation() {
      if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(showPosition);
      } else { 
          x.innerHTML = "Geolocation is not supported by this browser.";
      }
  }

  function showPosition(position) {
    var lat = position.coords.latitude;
    var lon = position.coords.longitude;

  getClosest(lat,lon);

  x.innerHTML = "Latitude: " + lat + 
      "<br>Longitude: " + lon;  


  }

 function getClosest(lat,lon) {
  r.innerHTML = "DSHFAOSIGIUSDOFDSJF";
    if (window.XMLHttpRequest) {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp = new XMLHttpRequest(); 
    } else {
      // code for IE6, IE5
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
   if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    txt.innerHTML = xmlhttp.responseText;
   }
  };
  xmlhttp.open("GET","test.php?lat="+lat+"&lon="+lon,true);
  xmlhttp.send();

  }
  </script>

这是我的php:

    <?php
error_reporting(E_ALL); ini_set('display_errors', 1);
echo "hello";


        echo "aghasldfha";
    $q = $_GET['lon'];
    $p = $_GET['lat'];

    $con = mysqli_connect('localhost','user','pass','db');
        if (!$con) {
            die('Could not connect: ' . mysqli_error($con));
        }

        mysqli_select_db($con,"db");
        $sql = "SELECT *, ( 3959 * acos( cos( radians('".$p."') ) * cos( radians( Latitude ) ) * cos( radians( Longitude ) - radians('".$q."') ) + sin( radians('".$p."') ) * sin( radians( Latitude ) ) ) ) AS distance FROM Events HAVING distance < 25 ORDER BY distance LIMIT 0 , 20";
        $result = mysqli_query($con,$sql);

        echo "<table>
        <tr>
        <th>Name</th>
        <th>Latitude</th>
        <th>Longitude</th>
        </tr>";
        while($row = mysqli_fetch_array($result)) {
            echo "<tr>";
            echo "<td>" . $row['Name'] . "</td>";
            echo "<td>" . $row['Latitude'] . "</td>";
            echo "<td>" . $row['Longitude'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";

?>

1 个答案:

答案 0 :(得分:0)

更新自评论 -

只需重构以使您的代码更具可读性并跟踪您从PHP发送的所有内容,您可以将所有echo语句组合成一个,并在完成所有操作后最后调用它。 / p>

试试这个 -

$q = $_GET['lon'];
$p = $_GET['lat'];

$con = mysqli_connect('localhost','user','pass','db');
    if (!$con) {
        die('Could not connect: ' . mysqli_error($con));
    }

    mysqli_select_db($con,"db");
    $sql = "SELECT *, ( 3959 * acos( cos( radians('".$p."') ) * cos( radians( Latitude ) ) * cos( radians( Longitude ) - radians('".$q."') ) + sin( radians('".$p."') ) * sin( radians( Latitude ) ) ) ) AS distance FROM Events HAVING distance < 25 ORDER BY distance LIMIT 0 , 20";
    $result = mysqli_query($con,$sql);

    $table =  "<table>
    <tr>
    <th>Name</th>
    <th>Latitude</th>
    <th>Longitude</th>
    </tr>";

    $rows = "";
    while($row = mysqli_fetch_array($result)) {
        $rows .= "<tr>"."<td>" . $row['Name'] . "</td>"."<td>" . $row['Latitude'] . "</td>"."<td>" . $row['Longitude'] . "</td>"."</tr>";
    }
    $htmlResult = $table.$rows."</table>";
    echo $htmlResult;