0

I'm new to javascript and I tried to follow this tutorial.

I have a textbox(input) and I want to use the jQuery's autocomplete by loading some data from MySQL.

This is my controller code :

public function autocomplete() {
    if($this->input->post('txt_nama'))
        $this->absensi_m->get_umat($this->input->post('txt_nama'));

    /*if (isset($_GET['term'])){
        $q = strtolower($_GET['term']);
        $this->absensi_m->get_umat($q);
    }*/
}

This is my model :

public function get_umat($word) {
    $this->db->select('nama', 'tanggal_lahir');
    $this->db->like('nama', $word);
    $query = $this->db->get('msumat');

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

And this is my javascript :

<script type="text/javascript">
        $(function(){
            $("#txt_nama").autocomplete({
                source: "autocomplete"
            });
        });
    </script>

I tried to inspect the javascript by using firefox's firebug and GC's developer tool, and this is what i got :

<input type="text" id="txt_nama" name="txt_nama" class="ui-autocomplete-input" autocomplete="off">

Notice that the autocomplete is off. I guess this is the problem so i tried to turn it on by adding this code :

$(document).ready(function() {
            $("#txt_nama").attr("autocomplete", "on");
        });

The autocomplete element is turned on when i add this code, but the autocomplete is still not working.

I also tried to use echo, but none of my echo is working :

if($query->num_rows() > 0)
    {
        echo num_rows();
        echo 'a';

        foreach($query->result_array() as $row)
        {
            $new_row['label'] = htmlentities(stripslashes($row['nama']));
            $new_row['value'] = htmlentities(stripslashes($row['tanggal_lahir']));
            //build array
            $row_set[] = $new_row;
        }
        echo json_encode($row_set);
        //return row_set;
    }

What am I missing?

NOTE : I just wondering about Routes, is it related to this error?because normally, people use controller/method in source: (JavaScript), but I can't do that because the generated route will have double controller (index.php/absensi/absensi/autocomplete), so I remove the controller and just use the method (source: "absensi")

0

3 Answers 3

2

I believe you're using the source option incorrectly. From the docs:

When a string is used, the Autocomplete plugin expects that string to point to a URL resource that will return JSON data.

If you meant to have source pointing to your autocomplete function, you need to give your controller name like they have on the demo you linked:

source: "birds/get_birds" // path to the get_birds method
Sign up to request clarification or add additional context in comments.

4 Comments

Also using site_url method from CI might help.
@MihaiIonescu Thanks for your help. I tried to use source: "<?php echo site_url(); ?>/absensi/autocomplete", it generate the same route like i just use "autocomplete" and its still not working :D
Thanks for your help :D I also used to believe that, but when i use something like "absensi/autocomplete" (absensi is my controller and autocomplete is a method in it) it will give me error 404 (because the generated route is "index.php/absensi/absensi/autocomplete" - note that the absensi is called twice, which is cause the 404 error), but if i just use "autocomplete" theres no error in my firebug/Dev console :D
Please see my edited question, really need help here. Thanks :D
1

Model

public function get_umat($word) {
    $this->db->select('nama', 'tanggal_lahir');
    $this->db->like('nama', $word);
    $query = $this->db->get('msumat');

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

Controller:

public function autocomplete() {
    if($this->input->post('txt_nama'))
        echo json_encode($this->absensi_m->get_umat($this->input->post('txt_nama')));

}

You should place echo json_encode() in autocomplete() but not in model..

1 Comment

Thanks for your help :D it seems your controller code give an error, im trying to solve it now :D
0

This is the answer :

  1. DONT use echo, it will break the jquery.

  2. Use if (isset($_GET['term'])) instead of $this->input->post()

  3. Use source: "<?php echo site_url('absensi/autocomplete'); ?>" in the jquery

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.