在这里,我尝试提前输入。它在 JavaScript 中工作。如果我连接数据库它不工作。请找出问题所在。有任何替代方法可以做到这一点
<html>
<head>
<link href="css/bootstrap.min.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div style="margin: 50px 50px">
<label for="product_search">Product Search: </label>
<input id="product_search" type="text" data-provide="typeahead">
<label for="product_search">Name Search: </label>
<input id="namesearch" type="text" data-provide="typeahead">
<label for="product_search">Test Search: </label>
<input id="search" name="search" type="text" data-provide="typeahead">
</div>
<script src="js/jquery-1.9.1.min.js"></script>
<script src="js/bootstrap-typeahead.js"></script>
<script>
$(document).ready(function($) {
// Workaround for bug in mouse item selection
$.fn.typeahead.Constructor.prototype.blur = function() {
var that = this;
setTimeout(function () { that.hide() }, 250);
};
$('#product_search').typeahead({
source: function(query, process) {
return ["Deluxe Bicycle", "Super Deluxe Trampoline", "Super Duper Scooter"];
}
});
$('#search').typeahead({
name: 'search',
remote: '/search.php?query=%QUERY'
});
$('#namesearch').typeahead({
source: function(query, process) {
return ["Ravindran", "Aravinthan", "Dakshesh"];
}
});
})
</script>
</body>
</html>
和我的 db search.php 在根文件夹中
<?php
$link = mysql_connect('localhost', 'root', '');
if (!$link)
{
die('Could not connect: ' . mysql_error());
}
if (!mysql_select_db('test'))
{
exit;
}
$text = mysql_real_escape_string($_GET['query']);
$sql = 'SELECT id,title FROM games WHERE name LIKE "%'. $text . '%" LIMIT 5';
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
{
$posts[] = array($row['id'] => $row['name']);
}
echo json_encode($posts);
mysql_close($link);
?>