1
votes

I want to implement auto-complete search in cakephp 2.5.6 using jquery,but when i enter words in search,it returns me error 404 not found.I think that it could not make in to my database.Please give some help.Thanks in advance. here is my controller named users

 class UsersController extends AppController{
 var $helpers = array('Bootstrap');
 var $name = 'Users';
 var $components = array('RequestHandler');
 public function home(){}
 public function find() {  
 $this->User->recursive = -1;
 if ($this->RequestHandler->isAjax()) {
 Configure::write ( 'debug', 0 );
 $this->autoRender = false;
 $this->layout = 'ajax';
  $results = $this->User->find('all', array('fields' => array('User.username'),
      'conditions' => array('User.username LIKE ' => '%'.$this->request->query['term'] . '%'),
      'group' => array('User.username'),
   ));
   $i=0;
        foreach($results as $result){
                $response[$i]['value']=$result['User']['id'];
                $response[$i]['label']=$result['User']['username'];
        $i++;
        }
echo json_encode($response);
 console.log($response);}}
}

and my view is:

 <fieldset>
 <legend></legend>
 <?php echo $this->Form->create('User', array('type'=>'post','action' => 'find'));

    echo $this->Form->input('username', array(
        'type' => 'text',
        'empty' => 'Pick a username',
        'label' => 'username',
       // 'option'=> $users,
        'id' => 'username',
        'autocomplete' => 'on'));

         echo $this->Form->submit('submit',array(
                          'class' => 'btn btn-primary',
                          'div' => false));
           ?>
      </fieldset>
      </div>
      <script>
  $(document).ready(function(){
 var username = $('#username');
 username.defaultText('Search for people');
  // Using jQuery UI’s autocomplete widget:
  username.autocomplete({
      minLength    : 1,
           source: '/cakephp/livesearch/users/find.json'
      });
       });
    // A custom jQuery method for placeholder text:
      $.fn.defaultText = function(value){
     var element = this.eq(0); 
      element.data('defaultText',value);
     element.focus(function(){
      if(element.val() == value){
     element.val('').removeClass('defaultText');
          }
      }).blur(function(){
       if(element.val() == '' || element.val() == value){
       element.addClass('defaultText').val(value);
     }
      });
     return element.blur();
        }
       </script>

I think that there is a problem in script where i am giving path to find function.It is not querying to database. The error is like:

    GET http://localhost/cakephp/livesearch/users/find.json?term=s 404 (Not Found). 

I am looking forward for some help.thanks.

2

2 Answers

1
votes

Try to replace this:

source: '/cakephp/livesearch/users/find.json'

for this:

source:'<?php echo $this->Html->url(array("controller" => "users","action"=> "find")); ?>'

I was having the same error 404 problem.It worked for me.

0
votes

You are using your find method in your controller as a view. That is possible but you need to do it correctly. Now cakephp cannot parse the route of your request.

look at http://book.cakephp.org/2.0/en/views/json-and-xml-views.html#

2 problems i can see so far you need to solve: Router::parseExtensions() and $this->set('_serialize' ...