CodeIgniter Forums

Full Version: Autocomplete not working....
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]dauber[/eluser]
Hi, all...I'm trying to add an autocomplete to a form I have.

In short, when someone types the name of a game, autocomplete should provide suggestions pulled from a MySQL table. I tried using the code/hints from this blog -- http://www.jamipietila.fi/codeigniter-an...th-jquery/ -- to no avail. I posted a message to said blog, but I wanted to post here too 1) to cover all bases, and 2) because folks here have so far been VERY helpful!

First, I know for autocomplete to work, you need to have this in config/config.php -- and I do:

Code:
$config['csrf_protection'] = FALSE;

Here's what I put in my view before the closing head tag ("suggestions" is a function I added to my "usergames" controller, and "gameTitle" is the form field I want autocompleted):

Code:
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
[removed][removed]
[removed][removed]
[removed]
$(document).ready(function() {
$(function() {
  $( "#gameTitle" ).autocomplete({
   source: function(request, response) {
    $.ajax({ url: "<?php echo site_url('usergames/suggestions'); ?>",
    data: { term: $("#gameTitle").val()},
    dataType: "json",
    type: "POST",
    success: function(data){
     response(data);
    }
   });
  },
  minLength: 2
  });
});
});
[removed]

Here's the "suggestions" function in my controller (the "title" referred to in $row->title refers to the "title" field in the database table I'm trying to pull from):

Code:
function suggestions()
        {
            $this->load->model('usergames_model');
            $term = $this->input->post('term',TRUE);

            if (strlen($term) < 2) break;

            $rows = $this->autocomplete_model->GetAutocomplete(array('keyword' => $term));

            $json_array = array();
            foreach ($rows as $row)
   array_push($json_array, $row->title);

            echo json_encode($json_array);
        }

Now, here's what I added into the model:

Code:
function GetAutocomplete($options = array())
    {
     $this->db->select('title');
     $this->db->like('title', $options['keyword'], 'after');
     $query = $this->db->get('aararity');
  return $query->result();
    }

"aararity" is the table I'm trying to pull from, and again, "title" is the name of the field in that table.

Basically, when I run the form, there's no autocompletion whatsoever. I'm thinking the "$options[keyword]" parameter might be the problem; I pretty much copied that right from the aforementioned blog, and I'm guessing that might be a vague placeholder used by the author.

What am I missing?

El Forum

[eluser]dauber[/eluser]
Oops...just realized that the controller is referencing "autocomplete_model," which....heh heh....doesn't exist. Smile Changed it to "usergames_model" and suddenly it works wonderfully. Smile