Last active
August 20, 2017 09:50
-
-
Save taleeb35/bea0ebb736e61bd797c8 to your computer and use it in GitHub Desktop.
Auto Complete Search in Cakephp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Controller Action | |
public function find() { | |
if ($this->request->is('ajax')) { | |
$this->autoRender = false; | |
$country_name = $this->request->query('term'); | |
$results = $this->Country->find('all', array( | |
'conditions' => array('Country.name LIKE ' => '%' . $country_name . '%') | |
'recursive' => -1 | |
)); | |
$resultArr = array(); | |
foreach($results as $result) { | |
$resultArr[] = array('label' =>$result['Country']['name'] , 'value' => $result['Country']['name'] ); | |
} | |
echo json_encode($resultArr); | |
} | |
} | |
// View and Jquery Code | |
<?php | |
echo $this->Form->create('Country', array('action' => 'find')); | |
echo $this->Form->input('name',array('id' => 'Autocomplete')); | |
echo $this->Form->submit(); | |
echo $this->Form->end(); | |
?> | |
<script type="text/javascript"> | |
$(document).ready(function($){ | |
$('#Autocomplete').autocomplete({ | |
source:'<?php echo $this->Html->url(array("controller" => "countries","action"=> "find")); ?>', | |
minLength: 2 | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment