[eluser]nlweb[/eluser]
This is my first attempt at trying to implement an autocomplete feature with jquery in codeigniter. Its been smooth before, but I am running out of ideas.
I have searched (almost) every thread on here, as well as other sites and this is the closest I can get it to working, but it still doesnt work right.
Using a popular tutorial online, I am using the following code:
Controller:
Code:
public function lookup()
{
$this->load->model('customer_model');
$term = $this->input->post('term',TRUE);
$rows = $this->customer_model->GetAutocomplete(array('keyword' => $term));
$keywords = array();
foreach ($rows as $row)
array_push($keywords, $row->keyword);
echo json_encode($keywords);
}
Model:
Code:
function GetAutocomplete($options = array())
{
$this->db->select('company_name');
$this->db->like('company_name', $options['keyword'], 'after');
$query = $this->db->get('customers');
return $query->result();
}
View:
Code:
< script type="text/javascript" >
$(function() {
$( "#tags" ).autocomplete({
source: function(req, add){
var term = $("#tags").val(); //Get input
$.ajax({
url: '<?php echo site_url('customers/lookup');?>',
dataType: 'json',
type: 'POST',
data: term,
success: function(data){
add(data);
}
});
},
minLength: 0,
delay:0
});
});
< /script >
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags">
</div>
This does not work. At all. However, If i put an array into "customers/lookup" and return that as json depending on $this->input->post('term',TRUE), it correctly displays the data. I am thinking its something in the model, but I have rewritten it about 5 times and its still not working.
I usually dont give up on something like this, but it has me beat. Any advice? I had to add the SOURCE function to get even results from the array I used. Without that in there, even the array wont work.