【问题标题】:Error passing information from javascript to query database with php使用 php 将信息从 javascript 传递到查询数据库时出错
【发布时间】:2015-11-22 05:38:57
【问题描述】:

我正在尝试使用 HTML5 Geolocation 获取用户的位置。有了我得到的纬度和经度,我想使用 php 查询数据库以获取 25 英里内的最近位置。我拥有的 javascript 可以获取用户位置以及打印出纬度和经度,但是当我调用我的 php 文件以获取接近的位置时,它似乎不起作用。

编辑 2: 似乎没有调用 getClosest(),因为示例打印输出甚至没有打印到我的 <p id="r">

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

提前致谢。这是我的 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>";

?>

【问题讨论】:

  • 在 php 文件顶部使用 error_reporting(E_ALL); ini_set('display_errors', 1); 来查看您是否在 php 中遇到任何错误。
  • 仍然没有任何显示
  • 我检查了xmlhttp.responseText,但我一无所获。我可能也没有正确使用它,但我认为问题出在 php 上,因为该页面甚至不会在 php 脚本的顶部回显一些简单的东西。我确实在第三个参数上将 xmlhttp.open 从 true 更改为 false ,但仍然没有。
  • 只需将echo "hello"; 放入一个新的 PHP 文件中,看看它是否适用于您的服务器...
  • Echo "hello"; 在我服务器上的一个新 PHP 文件中工作

标签: javascript php mysql xmlhttprequest


【解决方案1】:

从评论更新-

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

试试这个 -

$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;

【讨论】:

  • 我还是和以前一样,它单独工作,但当它被javascript调用时就不行了。
  • 你可以使用jquery AJAX ...吗?
  • 好像getClosest() 没有被调用
  • @Chandan:您能解释一下您所做的更改以及它如何解决问题吗?到底是什么问题?
  • “您在 PHP 代码中多次执行 echo,如果它是 AJAX 调用,这将不起作用” 为什么这不起作用?我对此表示高度怀疑。 @user3272438:如果答案不能解决您的问题,请不要接受。
猜你喜欢
  • 1970-01-01
  • 2011-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-04
  • 2012-08-17
  • 2014-09-18
  • 1970-01-01
相关资源
最近更新 更多