I am trying to use AJAX to auto complete the users text. Right now I got AJAX to return the results I need for the auto complete feature to the ‘suggestions’ div as the user types. However, what I am trying to accomplish is a dropdown coming out of the input field (“prod-name”) with suggestions as options where the user can simply click on their preferred suggestion and make it the ‘value’ of the input (similar to how google has search suggestions as you type, but a simplified version).
My markup sof far:
<script type="text/javascript">
function search(searchword) {
$('#suggestion').load('invoice-get-data.php?searchword=' + searchword);
}
</script>
</head>
<body>
<div class="prod-name">
<input onKeyPress="search(this.value)" type="text" id="prod-name"/>
<div id="suggestion"></div>
</div>
</body>
My AJAX code so far (invoice-get-data.php code snippet)
$searchword = $_GET['searchword'];
$results = mysql_query("SELECT * FROM products WHERE prod_name LIKE '".$searchword."%'") or die(mysql_error());
while($row = mysql_fetch_array($results)){
echo $row['prod-name'] . '<br>';
}
jquery ui provides a widget for such autocompletion : http://jqueryui.com/autocomplete/ .
The javascript side would be:
And the PHP side would be: