I am trying to implement an search function (auto complete) using jquery,jquery-ui in Codeigniter, but I am getting "JSON Parse unexpected Character error" when typing into the input field.
When calling /user/search_userautocomplete/test I do get back some values in JSON.
(With warning : The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must to be declared in the document or in the transfer protocol.)
Does it have something to do with Content-Type? I am really stuck for the last hours. I would really appreciate if someone knows what I am doing wrong. Thanks
Controller:
// Search User autocomplete
function search_userautocomplete($title)
{
if(!empty($title) || isset($title))
{
$this->db->like('firstname',$title);
//$this->db->or_like('lastname',$title);
echo json_encode( $this->db->get('users_profiles')->result() );
}
}
user_autocomplete.js
$(document).ready(
function() {
$('#search').keypress(
function(e) {
if (e.which == 13) {
e.preventDefault();
}
var searched = $('#search').val()
var fullurl = $('#hiddenurl').val()
+ 'user/search_userautocomplete/'
+ searched;
$.getJSON(fullurl, function(result) {
var elements = [];
$.each(result, function(i, val) {
elements.push(val.title)
})
$('#search').autocomplete({
source : elements
})
})
})
})
searchfield_userautocomplete (View)
<div class="row-fluid tagline">
<div class="span8">
<form>
<label for="search">Search</label> <input id="search" type="text">
<input value="" id="hiddenurl" type="hidden"> <input
value="Submit" type="submit">
</form>
<script src="<?php echo base_url();?>js/jquery.js"
type="text/javascript"></script>
<script src="<?php echo base_url();?>js/jquery-ui.js"
type="text/javascript"></script>
<script src="<?php echo base_url();?>js/user_autocomplete.js"
type="text/javascript"></script>
</div>
</div>