I've done a ton of research and I still don't get how to use jQuery ui autocomplete. The auto complete does not work and I'm not sure why. Any help would be appreciated it!
I have the following on the frontend. 1. jQuery correctly linked. 2. jQuery-ui correctly linked. 3. jQuery-ui css correctly linked.
<script>
$("#tags").autocomplete({
source: function(request, response){
$.post("/panel/tags.php", {data:request.term}, function(data){
response($.maps(data, function(item) {
return {
label: item.tagName,
value: item.tagID
}
}))
}, "json");
},
minLength: 2,
dataType: "json",
cache: false,
focus: function(event, ui) {
return false;
},
select: function(event, ui) {
this.value = ui.item.label;
/* Do something with user_id */
return false;
}
});
</script>
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags" size="50" />
</div>
On the backend:
$query_tags = "SELECT tagID, tagName FROM DCB_Tags WHERE tagName LIKE '".$_GET['tags']."%' ORDER BY tagName ASC LIMIT 10";
$result_tags = mysql_query($query_tags) or die ("Error: " . mysql_error());
$response = array();
$tags = array();
while($row=mysql_fetch_array($result_tags))
{
$tagID=$row['tagID'];
$tagName=$row['tagName'];
$tags[] = array('tagID'=> $tagID, 'tagName'=> $tagName);
}
header("content-type: text/javascript");
echo json_encode($tags);
exit();
Output for this is:
[{"tagID":"1","tagName":"art"},{"tagID":"4","tagName":"art shows"},{"tagID":"3","tagName":"artist"},{"tagID":"2","tagName":"design"}]
If you access the page by putting ?tag=art it correctly eliminates "design".