Welcome Guest, Not a member yet? Register   Sign In
result from autocomplete with ajax jquery json is not exactly
#1

[eluser]misakitran[/eluser]
I use autocomplete jquery-ui. Results responsed but not exactly.

Code:
//-------------------- JS
$( "#code" ).autocomplete({ //the recipient text field with id #username
        source: function( request, response ) {
            $.ajax({
                url: "http://localhost:82/shop/backend/product/suggestions",
                dataType: "json",
                data: {term : $('#code').val()},
                success: function(data){
                    if(data.response == 'true') {
                        response(data.code);
                    }
                }
            });
        },
        minLength: 1
    });

Code:
//-------------------- Controller
function suggestions()
    {
        $term = trim($this->input->post('term',TRUE));
        $rows = $this->Product_model->get_auto_complete($term);
        $data = array();
        
        if (count($rows)>0) {
            $data['response'] = "true";
            $data['code'] = array();
            foreach ($rows as $row) {
                array_push($data['code'], $row['code']);
            }
        }
        else {
            $data['response'] = "false";
        }
        $data['term'] = $term;
     echo json_encode($data);
    }

Code:
//-------------------- Model
function get_auto_complete($code)
    {
        $this->db->select('code');
        $this->db->from('products');      
        $this->db->like('code',$code,'after');
        $query = $this->db->get();        
        return $query->result_array();
    }

In my table, i have code list : S0001, S0002, A0001, A0002
But, when i press key in code field, all are been showed.
Please help me this error !
Thanks !
#2

[eluser]john_j[/eluser]
in your model, after this line $query = $this->db->get(); add this new line which is
echo $this->db->last_query(); exit; and run the code. This will display the sql. Run this sql on your database and decide if the sql query that you are using is correct.
#3

[eluser]misakitran[/eluser]
thanks ! i find my error ! thanks for your help !




Theme © iAndrew 2016 - Forum software by © MyBB