Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.