views:

100

answers:

1

I have implemented the auto complete functionality using the Ajax.autocompleter function of the Scriptaculous js framework. The code is working, but I get the entire list populated instead of populating only the entries that match with the letter I have specified. This is my code:

This is the js function to get the auto-suggested entries.

new Ajax.Autocompleter("autocomplete", "autocomplete_choices",
                        "http://localhost/FormBuilder/forms/autoComplete",{});

This is the auto complete box and the entries where the entries are populated.

<input type="text" id="autocomplete" name="autocomplete_parameter"/>
  <div id="autocomplete_choices" class="autocomplete"></div>

And this is the autoComplete action in the forms controller where get the list of users corresponding to the typed letter.

function autoComplete()
{
 $this->set('users',$this->User->find('all',array('fields'=>array('User.id','User.name'),
                    'conditions'=>array('User.name LIKE' => $this->data['User']['name'].'%')
             )
             )
    );
    $this->layout = "ajax";
}

But suppose if I type letter 'p', I get the entire user's list instead of displaying the ones starting with the letter 'p'. Why do I get this problem? Where have I gone wrong?

A: 

Well, I found out the answer.. Actually a mistake. I should add the paramName option to the Ajax.autocompleter function and should get the value in the $_REQUEST method in the controller function.

new Ajax.Autocompleter("autocomplete", "autocomplete_choices",
                       "http://localhost/FormBuilder/forms/autoComplete",
                        {paramName:"autocomplete"});

function autoComplete()
{
   $userName=$_REQUEST['autocomplete'];
   $this->set('users',$this->User->find('all',array(
                                     'fields'=>array('User.id','User.name'),
                          'conditions'=>array('User.name LIKE' => $userName.'%'))
                                                                    )
              );
   $this->layout = "ajax";
}

Now I get only the entries starting with that alphabet.

Angeline Aarthi