好的,所以我正在尝试使用 PHP、MySQL 和 AJAX 进行实时搜索。我不确定我是不是错了。我的数据库托管在 phpMyAdmin 上。数据库名称是 Info,我要访问的表是名称。
我的三个页面是 index.php connect.php 和 fetch.php Index.php
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<style>
#here
{
width:400px;
height:300px;
border: 1px solid grey;
display:none;
}
#here a{
display:block;
width:98%;
padding:1%;
font-size:20px;
border-bottom:1px solid grey;
}
</style>
<body>
<script src=jq.js></script>
<script src="jq.js">
$(document).ready(function(e)
{
$("search").keyup(function()
{
$("#here").show();
var x = $(this).val();
$.ajax(
{
type:'GET',
url:'fetch.php',
data: 'q='+x,
success:function(data)
{
$("#here").html(data);
}
,
});
});
});
</script>
<h1>Live Search</h1>
<input type="search" name="search" id="search">
<div id="here">
</div>
</body>
</html>
获取.php
<?php
if(!empty($_GET['q']))
{
include 'connect.php';
$q=$_GET['q'];
$query = "select * from names where names like '%$q%';";
while($output=mysqli_fetch_assoc($result))
{
echo '<a>'.$output['names'].'</a>';
}
$query = "select * from names";
}
获取.php
?>
<?php
$host="localhost";
$user="andremac96";
$password="";
$db="Info";
$conn = mysqli_connect($host,$user,$password,$db);
?>