I’m attempting to customize an autocomplete plugin for jquery. This should be extremely simple.
It is supposed to pull the list of names from autocomplete.php and then send to “lookup”. It works when I type it out manually, but when I try to get the names with the function getnames() it doesn’t work. Am I returning the data correctly? The php file works as well.
var options, a;
function getnames() {
$.ajax({
type:'POST',
dataType:'json',
data:{ },
url:'autocomplete.php',
timeout:1000,
success:function (data) {
return data;
}
});
}
// Doesn't work:
// DOC READY
$("#members").one("click", function () {
options = {
lookup:getnames()
};
a = $('#members').autocomplete(options);
});
// Works:
// DOC READY
$("#members").one("click", function () {
options = {
lookup:["name1","name2"]
};
a = $('#members').autocomplete(options);
});
autocomplete.php
<?
header("Content-Type: application/json", true);
$info = array("name1","name2");
echo json_encode($info);
?>
You have get the names first and wait for the ajax response. When it arrives you have to start the autocomplete.
E.g. (untested):