Priska Aprilia
Priska Aprilia

Reputation: 1149

jQuery autocomplete in Codeigniter

I want to make an autocomplete search box, where the result of the query depends two inputs : 1. the value of the search box itself (by user's input) 2. the selected value of a dropdown

You can see the detail from the codes below. My problem lies on the source of the autocomplete function. How can I append parameter to the url ? I tried to use string concatenation like

source: '<?php echo site_url("crowd/get_POIs?cat=");?>'+'some value'+'&q='+'some value'

but it did not work.

HTML :

<select name="selCat">
    <option>....</option>
</select>

<input class="col-3" type="text" id="searchPOI" name="searchPOI" />

Javascript :

$("#searchPOI").autocomplete({

    source: '<?php echo site_url("crowd/get_POIs");?>'
  });

Controller

function get_POIs(){
            //alert($_GET['term']);
            if (isset($_GET['cat']) && isset($_GET['q'])){;
                $cat = strtolower($_GET['cat']);
                $q = strtolower($_GET['q']);
                $this->crowd->get_POIs($cat,$q);
            }
}

Model

function get_POIs($cat, $q){
        $this->db->select('title, contentid');
        $this->db->from('attraction');
        $this->db->where('cat3 = "'.$cat.'"');
        $this->db->like('title', $q);
        $query = $this->db->get();

        if($query->num_rows > 0){
            foreach ($query->result_array() as $row){
                $new_row['label']=htmlentities(stripslashes($row['title']));
                $new_row['value']=htmlentities(stripslashes($row['contentid']));
                $row_set[] = $new_row; //build an array
            }

            echo json_encode($row_set); //format the array into json data
        }
}   

Upvotes: 0

Views: 690

Answers (1)

Saty
Saty

Reputation: 22532

You can can your parameter using get method by using ajax

 $("#searchPOI").autocomplete({
        source: function(request, response) {
            $.ajax({url: <?php echo site_url("crowd/get_POIs") ?>,
                data: {cat: "some value", q: "some value"},
                dataType: "json",
                type: "GET",
                success: function(data) {
                    response(data);
                }
            });
        }
    });

You need to correct your query and return data from your controller

Controller

function get_POIs(){
            //alert($_GET['term']);
            if (isset($_GET['cat']) && isset($_GET['q'])){;
                $cat = strtolower($_GET['cat']);
                $q = strtolower($_GET['q']);
                $data=$this->crowd->get_POIs($cat,$q);
                echo $data;
            }
}

Models

function get_POIs($cat, $q){
        $this->db->select('title, contentid');
        $this->db->from('attraction');
        $this->db->where('cat3',$cat);
        $this->db->like('title', $q);
        $query = $this->db->get();

        if($query->num_rows > 0){
            foreach ($query->result_array() as $row){
                $new_row['label']=htmlentities(stripslashes($row['title']));
                $new_row['value']=htmlentities(stripslashes($row['contentid']));
                $row_set[] = $new_row; //build an array
            }

            return json_encode($row_set); //format the array into json data
        }
}   

Upvotes: 1

Related Questions