Skip to content

Instantly share code, notes, and snippets.

@taleeb35
Last active August 20, 2017 09:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save taleeb35/bea0ebb736e61bd797c8 to your computer and use it in GitHub Desktop.
Save taleeb35/bea0ebb736e61bd797c8 to your computer and use it in GitHub Desktop.
Auto Complete Search in Cakephp
//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