当用户键入一封信时,如何使用ajax自动完成来显示MySQL数据库中的值
How to use ajax auto complete to display values from a MySQL database when user types a letter
我在显示MySQL中的数据时遇到问题。我希望当我在输入字段中键入一个字母时,来自数据库的值将显示相关值。
例如,我键入A,它将显示与该产品相关的所有值。以下是我的代码-请提供您的建议。
<!DOCTYPE>
<html>
<head>
<script type="text/javascript">
var hxmlhttp = false;
if(window.XMLHttpRequest){
//for newer browser except ie6
xmlhttp = new XMLHttpRequest();
}else{
//for ie6 and below
xmlhttp = new ActiveXObjectt("Microsoft.XMLHTTP");
}
function askGoogleSuggest(){
var input = document.getElementById("textField");
if(input.value){
getData("select.php?qu=" + input.value,"targetDiv");
}else{
var targetDiv = document.getElementById("targetDiv");
targetDiv.innerHTML = "<div></div>";
}
}
function getData(dataSource, divId){
if(xmlhttp){
var obj = document.getElementById(divId);
xmlhttp.open("GET",dataSource,true);
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status==200){
obj.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null);
}
}
</script>
</head>
<h2>Langpayan Trademark</h2>
<form >
Search for <input type="text" id="textField"
name="textField" onkeyup = "askGoogleSuggest()">
</form>
<div id="targetDiv">
<p>it will display values</p>
<div></div>
</div>
</body>
select.php
<?php
$dbc = mysqli_connect('localhost','root','black98765','activity_8a')
OR die("Cannot connect to MySql: ". mysqli_connect_error());
$display = "SELECT bug.* , product.*, hardware.hardware_brand, software.software_name, solution.solution_name, bug_hardware.*
FROM bug
JOIN product ON bug.product_no = product.product_no
JOIN bug_hardware ON bug.bug_no = bug_hardware.bug_no
JOIN hardware ON hardware.hardware_no = bug_hardware.hardware_no
JOIN software ON software.software_no = bug_hardware.software_no
JOIN solution ON solution.bug_no = bug.bug_no
WHERE product_name like '%on%'";
$re = mysqli_query($dbc, $display);
if($re){
echo "<table border=1 id='table_form'>
<tr>
<th>Product Name </th>
<th>Name of Bug </th>
<th>Brand Name</th>
<th>Operating System</th>
<th>Solution</th>
<tr>";
while($row = mysqli_fetch_assoc($re)){
echo "<tr>";
echo "<td>".$row['product_name']."</td>";
echo "<td>".$row['bug_name']."</td>";
echo "<td>".$row['hardware_brand']."</td>";
echo "<td>".$row['software_name']."</td>";
echo "<td>".$row['solution_name']."</td>";
echo "</tr>";
}
echo "</table>";
}else{
echo "Cannot fetch data";
}
?>
问题在对话中得到解决:
函数getData
在askGoogleSuggest
函数内部调用时错过了divId
参数。
此外,行WHERE product_name like '%on%'";
必须更改为WHERE product_name like '".$_GET['qu']."%'";
,以按照OP 的预期运行
相关文章:
- 仅在第一个结果中显示/隐藏MySQL结果函数
- 当用户键入一封信时,如何使用ajax自动完成来显示MySQL数据库中的值
- 使用jquery显示mysql查询结果
- 从getTime()类型的日期获取日期和时间,以显示mySQL数据库中的日期
- 使用html表显示MySql数据
- 我无法获取显示mySql数据的Slickgrid
- 使用 php 和 Jquery 数据表显示 mysql 数据库
- 使用 JQuery 显示 MySQL 数据库
- 使用 Javascript 在选择框更改时显示 MySQL 列数据
- 在警报对话框中显示Mysql结果
- I'我正在尝试建立一个基本的网站来显示MySQL中的数据.有没有办法只使用javascript和HTML来实现这
- 仅显示mysql数据库中的格式化文本
- 如何在一行中显示MySQL中的列以及每个项的计数器
- 谷歌地图,显示mysql信息在标记信息窗口
- 显示mysql数据
- 在更改页面时显示mysql查询
- Highcharts使用JSON - graph不显示mysql数据
- 显示mysql表数据在信息窗口谷歌地图API
- 优化在node.js中显示mysql查询结果的效率
- extjs4:显示Mysql blob值中的图像