I am trying to make a autocomplete textbox that uses Bootstrap's typeahead functions. I have tested it through an array in javascript, manually typed. However I tried to add code that would allow autocomplete to use data from a mysql database, but I can't get this to work. What can I do to fix this so that autocomplete will show suggested words from a MySQL database?
HTML (test2.html)
<div class="well">
<input type="text" class="span3" id="typeahead" data-provide="typeahead" data-items="4" />
</div>
<script>
$('#typeahead').typeahead({
source: function(typeahead, query){
$.ajax({
url: 'source.php',
type: 'POST',
data: 'query=' + query,
dataType: 'JSON',
async: 'false',
success: function(data){
typeahead.process(data);
}
});
}
});
</script>
PHP (source.php)
<?php
include 'connect.php'; // connects to database
if (isset($_POST['query'])) {
$query = $_POST['query'];
$mysql_query = mysql_query("SELECT * FROM country WHERE country LIKE '%{$query}%'");
while ($row = mysql_fetch_assoc($mysql_query)) {
$array[] = $row['name'];
}
json_cnode($array)
}