我正在尝试做一个自动建议的搜索框,我的一切正常,但我找不到一种方法,以便当我点击列表中的一个项目时,让该文本进入文本框。一旦发生这种情况,我还需要列表消失。有谁知道如何做到这一点?请在附件中找到我的代码:
索引.php
<html>
<head>
<script type="text/javascript">
function findmatch() {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById('results').innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open('GET', 'search.inc.php?search_text='+document.search.search_text.value, true);
xmlhttp.send();
}
</script>
</head>
<body>
<form id="search" name="search">
Type a name:<br />
<input type="text" id = "search_text" name="search_text" onkeyup="findmatch();">
</form>
<ul>
<div id="results"></div>
</ul>
</body>
</html>
搜索.inc.php
<?php
if (isset($_GET['search_text'])) {
$search_text = $_GET['search_text'];
}
if (!empty($search_text)){
if (@mysql_connect('localhost','username','password')) {
if (@mysql_select_db('Database')){
$query = "SELECT name FROM Customers WHERE name LIKE '$search_text%'";
$query_run = mysql_query($query);
while ($query_row = mysql_fetch_assoc($query_run)){
$id = "id".$i;
echo "<li>".$name = $query_row['name']." </li>";
}
}
}
}
?>