作为一个非开发人员,我花了一些时间才能做到这一点 - 就目前而言,这个jQuery自动完成功能适用于我的目的。 但我没有运气编码将“无匹配”消息返回到搜索结果列表中。任何指导赞赏。我查看了jQuery自动完成文档,但看不到答案。
<script>
$(document).ready(function() {
$(function() {
$("#catalog").autocomplete({
source: 'search-php.php',
select: function(event, ui) {
$(event.target).val(ui.item.value);
$('#searchform').submit();
return false;
},
minLength: 3
});
});
});
<input type="search" id="catalog" name="track" size="30" maxlength="188" maxlength="50" spellcheck="false" autocorrect="off" autocomplete="off" style="text-decoration:none;color:#fff;" placeholder="Start typing ..." autofocus>
//connect with the database
$db = new mysqli($dbHost,$dbUsername,$dbPassword,$dbName);
//get search term and then remove all hyphens, spaces and apostrophes
$searchTerm = $_GET['term'];
$searchTerm = str_replace('-', '', $searchTerm);
$searchTerm = str_replace(' ', '', $searchTerm);
$searchTerm = str_replace("'", "", $searchTerm);
$searchTerm = str_replace(',', '', $searchTerm);
//get matched data from table which ignores all hyphens, spaces and apostrophes
$query = $db->query("SELECT track FROM bsk001_cat_
WHERE replace(replace(replace(replace(track,'-',''),'\'',''),' ',''),',','') LIKE '%$searchTerm%'
ORDER BY track ASC");
while ($row = $query->fetch_assoc()) {
$data[] = $row['track'];
}
//return json data
echo json_encode($data);
&GT;