【发布时间】:2011-06-02 01:16:48
【问题描述】:
我有一个 PHP 搜索建议脚本,它使用 MySQL 数据库作为其后端,并使用 jQuery 将内容推送到搜索框页面。 PHP 目前在Suggest.php 中,我的搜索框在index.php 中。我想将来自Suggest.php 的PHP 脚本放入index.php 脚本代码中,但它似乎不起作用。为什么会这样?
这是我的Suggest.php代码:
<?php
$database=new mysqli('localhost','username','password','database');
if(isset($_POST['query'])){
$query=$database->real_escape_string($_POST['query']);
if(strlen($query)>0){
$suggestions=$database->query("SELECT name, value FROM search WHERE name LIKE '%".$query."%' ORDER BY value DESC LIMIT 5");
if($suggestions){
echo '<ul id="suggest">';
while($result=$suggestions->fetch_object()){
echo '<li>'.$result->name.'</li>';
}
echo '</ul>';
}
}
}
?>
这是我的 index.php 代码:
<script type='text/javascript' src='js/jquery.js'></script>
<script type='text/javascript'>function lookup(a){if (a.length==0){$("#suggestions").hide();} else {$.post("suggest.php",{query:"" + a + ""},function (b){$("#suggestions").html(b).show();})}};</script>
<form id='search' method='post'>
<input type='text' id='query' onkeyup='lookup(this.value);'>
<div id='suggestions'></div>
</form>
【问题讨论】: