Codeigniter + Autocomplete Jquery UI does not return list

Asked

Viewed 715 times

0

Good afternoon, everyone,

I have a search field where I give "suggestions" of what to type, based on what I have registered in my database. The same was working perfectly, but it stopped working and I can’t 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 test call this way in the browser http://localhost/articles/sugestao? term=Profanity it returns the result normally, but at the time I type in the input it does not show the listing as it was done before.

Thanks in advance!

2 answers

1

You’ll have to use AJAX to do this, as you are using $_GET to 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
});
  • didn’t work this way :/

  • Could you tell what happens? So we can see if a lolusion better

  • Thanks for the help... with this code he gave me he doesn’t even bring results, when I try to type something he doesn’t throw me anything in the Chrome console, with what is it brings me the request in the console and if I access the URL it creates it returns me the results it just does not display below the input these results.

  • @Davidrodrigues I understand I’ll do some tests

0

David, I see you’re using the autocomplete. If the proposed result is brought via browser, the problem is in Jquery.

Check:

  • If the Chrome element inspector console displays an error (Chrome > F12)
  • If the field with id #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 suddenly you modified something and do not remember or even a simple .htaccess may have spoiled the fun.

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

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

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

  • I checked in the Chrome console and it returns no error. Does the query field exist and the <?php echo site_url('articles/sugestao'); ? > returns the url normally, also tried to pass the full url and nothing. As I explained earlier it was working and I don’t remember modifying anything that would affect it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.