jQuery UI Autocomplete not working with codeigniter

I’m trying to get a field on my view to autocomplete with values from a database but can’t seem to figure out what is going wrong….In my view I have the following script:$(document).ready(function() $(function() $( “#searchQuestion” ).autocomplete( source: function(request, response) $.ajax( url: “<?php echo site_url(‘contentmanagement/suggestions’); ?>…

via Sql Databases Development » Search Results » ajax:

jQuery UI Autocomplete not working with codeigniter

I’m trying to get a field on my view to autocomplete with values from a database but can’t seem to figure out what is going wrong….
In my view I have the following script:
$(document).ready(function()
$(function()
$( “#searchQuestion” ).autocomplete(
source: function(request, response)
$.ajax( url: ““,
data: term: $(“#searchQuestion”).val(),
dataType: “json”,
type: “POST”,
success: function(data)
response(data);

});
},
minLength: 2
});
});
});Within my contentmanagement controller I have the “suggestions” function:
function suggestions()
$this->load->model(‘onlinehelp’);
$term = $this->input->post(‘term’, TRUE);

if (strlen($term) < 2)
break;

$rows = $this->onlinehelp->GetAutocomplete($term);

$keywords = array();
foreach ($rows as $row)
array_push($keywords, $row->question);

echo json_encode($keywords);And Finally within my model I have the follow function –
function GetAutocomplete($term)
$this->db->select(‘question’);
$this->db->like(‘question’,$term, ‘both’);
$query = $this->db->get(‘question’);
return $query->result();
The query above is the equivalent to “SELECT question FROM question WHERE question LIKE %$term%.
Can anyone see where I am going wrong with this??
……………………………………

You might be getting a 500 Internal Server Error caused by CSRF protection being enabled. If so, every POST request must contain a CSRF value. You have a few options:
1. Include the CSRF value in your data using $this->input->cookie(‘your_csrf_name’);
2. Perform GET request instead of POST.
Use $this->input->get(‘term’, TRUE); in your controller. Remember to sanitize and validate the value.
3. Disable CSRF protection. Not recommended.
……………………………………

This works with the CSRF enabled:
Use the jquery cookie plugin
Then on your autocomplete thing:

For more info: jQuery UI Autocomplete not working with codeigniter

Sql Databases Development » Search Results » ajax

jQuery UI Autocomplete not working with codeigniter

Share this post:

Related Posts

Leave a Comment