【问题标题】:MySQL query result doesn't show using AJAXMySQL 查询结果不显示使用 AJAX
【发布时间】:2017-12-13 03:33:52
【问题描述】:

我有一个名为opera_house 的数据库,其中有一个名为room 的表,其中有一个字段room_name 和一个字段容量。我想显示 room_name 的容量大于用户输入的容量。

Available Room 文本消失了,但如果我回显它,我的代码只会显示 MySQL 查询,但我不确定它是否正在搜索数据库。

这是我的脚本代码:

<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
    function showRoom(str) {
        if (str === "") {
            document.getElementById("txtHint").innerHTML = "";
            return;
        } else { 
            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 (this.readyState === 4 && this.status === 200) {
                    document.getElementById("txtHint").innerHTML = this.responseText;
                }
            };
            xmlhttp.open("GET","ajax_events.php?q="+str,true);
            xmlhttp.send();
        }
    }

这是我的html:

<body>
    <form>
        <input type="text" name="room" onkeyup="showRoom(this.value)">
    </form>
    <br>
    <div id="txtHint"><b>Available Room...</b></div>
</body>

这是我的 php:

<?php
    include('dbconnect.php');
    $q = intval($_GET['q']);

    mysqli_select_db($connection,"opera_house");
    $sql="SELECT room_name FROM room WHERE capacity >= '".$q."'";
    echo $sql;
    $result = mysqli_query($connection,$sql);

    while($row = mysqli_fetch_array($result)) {
        echo "<td>" . $row['room_name'] . "</td>";
    }
?>

我的 php 文件名为 ajax_events.php

我的 dbconnect.php 是我经常用来连接到这个数据库的一个。

非常感谢一些帮助!

【问题讨论】:

  • 你有什么错误吗?
  • 尝试从浏览器运行 php 文件以查看代码返回的内容。
  • 您已经嵌入了 jQuery,但您仍然在处理原生 XMLHttpRequest 实现的所有麻烦吗?你开玩笑的吧。请这边走,api.jquery.com/jquery.ajax ...

标签: php html mysql ajax


【解决方案1】:

我建议使用jquery 回答。您已将它嵌入到您的问题中,但您没有使用它...

说明:仅当定义了str 时,才使用参数"q" 调用以下url ajax_events.php,否则它将不填充选择器txtHint

AJAX

if (str != "") {
  $.ajax({
    type: 'GET',
    url: 'ajax_events.php',
    dataType: 'JSON',
    data : {
      q: str
    }
  }).done(function (data) {
    $('#txtHint').text = data;
  }).fail(function() {
    alert('Fatal error');
  })
} else {
  $('#txtHint').text = '';
}

使用此配置,在您的服务器端代码中返回带有echo json_encode 的结果很重要。

PHP

<?php
  include('dbconnect.php');
  $q = intval($_GET['q']);

  mysqli_select_db($connection, "opera_house");
  $sql = 'SELECT room_name FROM room WHERE capacity >= '.$q; // Some corrections
  $result = mysqli_query($connection, $sql);

  $return = '';
  while($row = mysqli_fetch_array($result)) {
    $return .= '<td>' . $row["room_name"] . '</td>';
  }
  echo json_encode($return); // Return Json to ajax
?>

【讨论】:

    【解决方案2】:

    虽然想到 JS,但它很好。我认为问题出在php代码中。试试这个。

     <?php
        include('dbconnect.php');
        $q = intval($_GET['q']);
    
        mysqli_select_db($connection,"opera_house");
        $sql="SELECT room_name FROM room WHERE capacity >= " . $q;
    
        $result = mysqli_query($connection,$sql);
    
        if (!$result) {
            echo mysqli_error(); 
            exit();
        } // this is to do debugging. remove when you get it fixed
    
        $ret = ""; //variable to hold return string
        while($row = mysqli_fetch_array($result)) {
           $ret .= "<td>" . $row['room_name'] . "</td>";
        }
    
     echo $ret;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-04
      • 1970-01-01
      • 2013-01-31
      • 1970-01-01
      • 1970-01-01
      • 2017-09-14
      • 2014-02-02
      相关资源
      最近更新 更多