<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>AutoComplete</title> <script> $(function() { $("#tags").autocomplete({ source: "Search.php", minLength: 2, //search after two characters select: function( event, ui ) { console.log( ui.item ? "Selected: " + ui.item.value + " aka " + ui.item.id : "Nothing selected, input was " + this.value ); } }); }); </script> </head> <body> <label for="tags">Tags:</label> <input id="tags"> </body> </html>
Search.php
$term = trim(strip_tags($_GET['term'])); $arr = array(); $sql = "SELECT id,tag FROM test WHERE tag LIKE '".$term."%'"; $data = $this->db->getAll($sql); foreach($data as $k => $v) { $arr[$k]['id'] = $v['id']; $arr[$k]['value'] = $v['tag']; } echo json_encode($arr);