Codeigniter + Autocomplete JQuery UI does not return list

0

Good afternoon guys,

I have a search field where I give "suggestions" rather than typing, based on what I have registered in my database. It was working perfectly, but it stopped working and I can not find the error.

Script:

<script>
    $(function(){
        $('#query').autocomplete({
            source: "<?php echo site_url('artigos/sugestao'); ?>"
        });
    });
</script>

Controller:

public function sugestao()
{
    if(esta_logado() == TRUE){
        if(isset($_GET['term'])){
            $result = $this->artigos->sugestao($_GET['term']);
            if(count($result) > 0){
                foreach ($result as $r)
                    $arr_result[] = $r->titulo;
                echo json_encode($arr_result);
            }
        }
    } else {
        redirect('/login');
    }
}

Model:

public function sugestao($palavraChave)
{
    $query = $this->db->query("//select que utilizo");
    return $query->result();
}

If I try calling this way in the browser link AnyQuery it returns me the result normally, but the moment I type in the input it does not show the listing as it was done before.

Thank you in advance!

    
asked by anonymous 23.06.2015 / 22:18

2 answers

1

You will have to use AJAX to do this, as you are using $_GET to do the search you will need to use $.get()

jQuery("#query").autocomplete({
    source: function (request, response) {
        jQuery.get("<?php echo site_url('artigos/sugestao'); ?>", {
            term: request.term
        }, function (data) {
            response(data);
        });
    },
    minLength: 3 // define o número mínimo de caracteres digitados antes de fazer a busca
});
    
23.06.2015 / 23:05
0

David , I saw that you're using autocomplete . If the proposed result is brought via browser, the problem is in JQuery.

Check:

  • If the Chrome Elements inspector console displays an error (Chrome & Ft)
  • If the field with #query exists
  • If the path brought by <?php echo site_url('artigos/sugestao'); ?> is valid (display the source code on the page and see the URL explicitly expressed), because you suddenly modified something and do not remember or even a simple .htaccess may have spoiled the game .

You can also manually execute the request (just paste and give ENTER) on the Chrome element inspector console (F12):

$('#query').autocomplete({
    source: "URL_COMPLETA_AQUI"
});

In this way you can see the return or error that JQUERY might pop.

    
24.06.2015 / 02:30