I'm trying to create a search suggestion input box with Cakephp 3. I found a tutorial online and i'm trying to implement it but i don't get a suggestion as mentioned in the tutorial. (http://www.naidim.org/cakephp-3-tutorial-18-autocomplete)
PagesController.php
public function chargesadd()
{
$this->loadModel('Charges');
if ($this->request->is('ajax')) {
$this->autoRender = false;
$name = $this->request->query['term'];
$results = $this->Customers->find('all', [
'conditions' => [ 'OR' => [
'name LIKE' => $name . '%',
'id LIKE' => $name . '%',
]]
]);
$resultsArr = [];
foreach ($results as $result) {
$resultsArr[] =['label' => $result['name'], 'value' => $result['id']];
}
echo json_encode($resultsArr);
}
}
chargesadd.ctp
<?php use Cake\Routing\Router; ?>
<?php
echo $this->Form->input('customer_id', ['type' => 'text']);
?>
<script>
jQuery('#customer-id').autocomplete({
source:'<?php echo Router::url(array('controller' => 'Pages', 'action' => 'chargesadd')); ?>',
minLength: 3
});
</script>
I have a feeling there's something wrong with the $resultsArr how it's been outputted. But i can't properly figure out what's wrong with it.
I'm using CakePHP 3.4.7