【发布时间】: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 ...