I want to make auto complete text box for select employee names from the database. I don't have fair idea about that. But I tried to make it as following.
autocomplete.php
<?php
include 'func/db_connect.php';
if (isset($_POST['query']))
{
$query = $_POST['query'];
$mysql_query = mysql_query("SELECT * FROM employee WHERE name LIKE '%{$query}%'");
while ($row = mysql_fetch_assoc($mysql_query))
{
$array[] = $row['name'];
}
echo json_encode ($array);
}
js script
<script>
$('#typeahead').typeahead({
source: function(typeahead, query){
$.ajax({
url: 'autocomplete.php',
type: 'POST',
data: 'query=' + query,
dataType: 'JSON',
async: 'false',
success: function(data){
typeahead.process(data);
}
});
}
});
</script>
index.php
<link rel="stylesheet" href="css/jquery-ui-1.10.3.custom.min.css" />
<script src="js/jquery-1.10.2.min.js"></script>
<script src="js/jquery-ui-1.10.3.custom.min.js"></script>
<td><b>Employee name : </td>
<td>
<input type="text" id="typeahead" data-provide="typeahead" size="30">
</td>
But it does not work. What is the correct way of make autocomplete text box.