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.