I’m using some Javascript I found from a post on StackOverflow. When I start entering text into the input, a spiny loading graphic appears within the input element, however nothing actually appears.
When I go to my URL that returns the JSON encoded string just to test it, it seems to work fine:
http://xxxxxxxx/app/get_clients/test (get_clients is the function, test is the string to search for)
Returns:
[“Testing”,”Testing1″,”test11″,”test4″,”Testing21″,”Just Testing”,”testy”]
Any idea what I’m doing wrong?
Here is my JavaScript:
format_item = function (item, position, length)
{
return item.title;
}
prep_data = function(data)
{
tmp = $.evalJSON(data);
parsed_data = [];
for (i=0; i < tmp.length; i++)
{
obj = tmp[i];
parsed_data[i] =
{
data: obj ,
value: obj.isbn13,
result: obj.title
};
}
return parsed_data
}
$(document).ready(function()
{
$("#file_hide").hide();
$("#<?php echo $this->form_name['company']; ?>").autocomplete({
url : "<?php echo site_url("app/get_clients"); ?>",
parse: prep_data,
formatItem: format_item,
});
});
And my PHP:
function get_clients($s)
{
$this->db_common->like('name', $s);
$query = $this->db_common->get('clients');
$results = array();
foreach ($query->result_array() as $row):
$results[] = $row['name'];
endforeach;
echo json_encode($results);
}
Demo page has link to sample php backend implementation.
http://jquery.bassistance.de/autocomplete/demo/
In short, you need to use request parameter (q by default).