I am using jQuery autocomplete, and want to have a separate PHP file to house the data, instead of creating a variable (i.e. var availableTags = ["Red", "Orange", "Yellow", "Green, "Blue"]).
However, when doing this, the autocomplete will continually display all of the available options, and not filter out what the user types in.
Any ideas?
Javascript:
$(document).ready(function() {
$( ".autocomplete" ).autocomplete({
source: "../../db/autocomplete_list.php"
});
});
autocomplete_list.php
require_once('../includes/connect.php');
$dbh = connect(DB_HOST,DB_USER,DB_PASSWORD,CM_DATABASE);
$colors = '[';
$sth = $dbh->query ("SELECT * FROM constants
WHERE category='Colors'
ORDER BY display");
while ($row = $sth->fetch ()) {
$colors.= '"'.$row[value].'", ';
}
$colors = rtrim($colors, ', ');
$colors .= ']';
print $colors;
execution of autocomplete_list.php
["Red", "Orange", "Yellow", "Green, "Blue"]
EDIT:
Have updated my autocomplete_list.php file to the following. However, now the autocomplete isn't displaying my values. If I check the response in FireBug, it is showing the results correctly, it just appears that they aren't being passed back to the autocomplete.
require_once('../includes/connect.php');
$dbh = connect(DB_HOST,DB_USER,DB_PASSWORD,CM_DATABASE);
if(isset($_GET['term'])) {
$mysearchString = $_GET['term'];
}
$sth = $dbh->query("SELECT category, value, display FROM constants
WHERE category = 'COLORS' AND value LIKE '$mysearchString%'
ORDER BY display");
while ($row = $sth->fetch ()) {
print $row['display']."\n";
}