amidamaru
amidamaru

Reputation: 43

how to change type ajax call to controller of option "serviceUrl" in ajax autocomplete jquery devbridge with CodeIgniter?

I'm using ajax autocomplete tutorial from http://www.devbridge.com/projects/autocomplete/jquery.

I wanna change query of it to get exactly when I fill input text. So, I need to write a function in model such as: "select username from user where username LIKE "search_string%" ".

But now my first problem is, it sends request to server: http://localhost/project_name/controller_name/function_name?query=search_string.

In CodeIgniter must be: http://localhost/project_name/controller_name/function_name/search_string .

My second problem, how can I get and pass search_string from script initialize autocomplete?

MY HTML:

<input class="input-short" style="width: 150px;" type="text" id="search_by_user"  />

MY SCRIPT:

var options, a;
$(document).ready(function(){
    var search_string = ???; //how to get??
    options = {
        serviceUrl:'controller_name/function_name/' + search_string, //**how to get search_string???**
        onSelect: function(suggestion) {
            alert('You selected ' + suggestion.value);    
        } 
    };
    a = $('#search_by_user').autocomplete(options);   
});

How to fix those?

Upvotes: 1

Views: 3011

Answers (2)

Kurniawan Ismail
Kurniawan Ismail

Reputation: 11

remove data[]
just    
$suggestions[] = array('value'= $row['fildname'],'data'=>$row['id']);

Upvotes: 1

amidamaru
amidamaru

Reputation: 43

I fixed my problems, but data that contains values for callback function when data is selected always null. What's up?
MY SCRIPT:

var options, a;

$(document).ready(function(){
    options = {
        serviceUrl:'controller_name/function_test',
        onSelect: function(suggestion) {
            alert('You selected ' + suggestion.value + ', ' + suggestion.data);    
        } 
    };
    a = $('#search_by_user').autocomplete(options);   
});

MY FUNCTION IN CONTROLLER :

public function function_test() {
        $query = isset($_GET['query']) ? $_GET['query'] : FALSE;
        //$query = 'te';
        if($query) {
            $found = $this->model_name->get_list($query);
            $suggestions = array();
            $data = array();
            foreach($found as $row) {
                $suggestions[] = $row['strfound'];
                $data[] = $row['id'];
            }
            $response = array(
                'query' => $query,
                'suggestions' => $suggestions,
                'data' => $data,
            );
            echo json_encode($response);
        }
}

When I echo $response to browser to test, I got data.
But when I make real, suggestion.data always returns null. Why?
EX: I got alert the same as "You selected teo, null".

Upvotes: 1

Related Questions