I'm trying to do an autosuggest search box and I have everything working but I can't find a way so that when I click on one of the items in the list to have that text go into the textbox. I also need the list to disappear once once this happens. Does anybody have an idea how to do this? Please find my code attached:
Index.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>
Search.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>";
}
}
}
}
?>
You aren't calling your Clicked() function from anywhere. Try adding an event to the list items you are outputting:
echo "<li onclick='Clicked(this)'>".$name = $query_row['name']." </li>";
And you will also want to know what was clicked in your Clicked function"
function Clicked(li)
{
document.getElementById("aaa").value = li.innerHTML;
document.getElementById("results").innerHTML = '';
return false; // so no postback
}