Skip to content

Instantly share code, notes, and snippets.

@swthate
Created October 17, 2014 14:22
Show Gist options
  • Save swthate/b03d4650c84e9d240e16 to your computer and use it in GitHub Desktop.
Save swthate/b03d4650c84e9d240e16 to your computer and use it in GitHub Desktop.
autocomplete errors
<h1><?php echo __('Auto-complete');?></h1>
<?php
echo $this->Form->create('JqueryExample', [
'url' => [
'controller' => 'jquery_examples',
'action' => 'autocomplete'
]
]);
echo $this->Form->input('term', ['label' => __('Search for an animal')]);
echo $this->Form->submit(__('Search'));
echo $this->Form->end();
?>
<?php $this->append('css');?>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/themes/smoothness/jquery-ui.css" />
<?php $this->end();?>
<?php $this->append('script');?>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/jquery-ui.min.js"></script>
<script>
$(function () {
$('#JqueryExampleTerm').autocomplete({
$.ajax({
url: $('#JqueryExampleAutocompleteForm').attr('action') + '.json',
datatype: 'json',
data: {
'term': $('#JqueryExampleTerm').val()
},
success: function (data) {
response(data.items);
},
minLength: 2,
select: function (event, ui) {
// If you want to do something when a user clicks an item
},
open: function () {
$(this).removeClass('ui-corner-all').addClass('ui-corner-top');
},
close: function () {
$(this).removeClass('ui-corner-top').addClass('ui-corner-all');
}
});
});
});
</script>
<?php $this->end();?>
Error: The action autocomplete.ctp is not defined in controller JqueryExamplesController
Error: Create JqueryExamplesController::autocomplete.ctp() in file: app\Controller\JqueryExamplesController.php.
<?php
class JqueryExamplesController extends AppController {
public function autocomplete.ctp() {
}
}
<?php
// JqueryExamplesController.php
class JqueryExamplesController extends AppController {
/**
* Example of an autocomplete field using jQuery UI
* with JSON and AJAX
*
* @author DerEuroMark
*/
public function autocomplete() {
if ($this->request->is(array('ajax'))) {
$this->loadModel('Animal');
$items = $this->Animal->find('list', array(
'conditions' => array(
'name LIKE' => '%' . $this->request->query('term') . '%'
)
));
$this->set('items', $items);
$this->set('_serialize', array('items'));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment