• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Autocomplete not working....

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:

$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):

<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
$(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){
  minLength: 2

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):

function suggestions()
            $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:

function GetAutocomplete($options = array())
     $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?

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

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.