Harris Khan Harris Khan - 1 year ago 80
Ajax Question

autocomplete search in codeigniter

Hy, i am making an autocomplete search field in codeigniter,

Here is my view:

<!doctype html>
<meta charset="utf-8">
<title>Untitled Document</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js" ></script>

$(document).ready(function() {
$( "#text" ).autocomplete({
source: function(request, response) {
url: "http://localhost/new/index.php/travels/search_fields",
data: { term: $("#text").val()},
dataType: "json",
type: "POST",
success: function(data){
minLength: 2

<form method="post">
<input type="text" name="text" id="text" autocomplete="off" >

Here is my controller:

function search_fields(){
$term = $this->input->post('term', TRUE);
$data['var']= $this->Travel->search_field($term);
echo json_encode($data['var']);


and here is my model:

function search_field($term){
$this->db->like('destination', $term);
$query = $this->db->get();
return $query->result();


I am getting an empty drop down list, where I am doing wrong?? is there anything wrong with ajax or library etc. Please anyone help me out.

Answer Source

In Model return result_array :

return $query->result_array();

In Controller :

$search_data = $this->Travel->search_field($term); 
echo json_encode($search_data);

Some change after ajax success :

success: function(data){
   var resp = $.map(data,function(obj){
        return obj.destination;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download