我有一個PHP搜索建議腳本,它使用MySQL數據庫作爲其後端和jQuery將內容推送到搜索框頁面。 PHP目前在suggest.php中,我的搜索框在index.php上。我想將來自suggest.php的PHP腳本放入index.php腳本代碼中,但它似乎不起作用。爲什麼會這樣呢?將PHP腳本與另一個PHP文件,jQuery和HTML結合起來
這裏是我的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>