I'm creating a web application for work where the user has to enter the name of the person that requested the job. I'd like to create a simple AJAX auto-suggest dropdown so they don't need to type the entire name. On the backend, the database will provide suggestions based on previous entries. The website is built using CakePHP 1.1.
I know there are a lot of libraries out there, some better than others. Which do you think is the fastest and easiest to implement?
Since you are using CakePHP 1.1 I suggest you check out the Manual portion that deals with Helpers
If you go down to 'AJAX', you can see you can do something like this in your controller:
function autocomplete () {
$this->set('people',
$this->Person->findAll("name LIKE '%{$this->data['Person']['name']}%'")
);
$this->layout = "ajax";
}
And in your autocomplete.thtml
view, you would have:
<ul>
<?php foreach($people as $person): ?>
<li><?php echo $person['Person']['name']; ?></li>
<?php endforeach; ?>
</ul>
And to create the autocomplete field in another view, you would do:
<form action="/people/index" method="POST">
<?php echo $ajax->autoComplete('Person/name', '/people/autocomplete/')?>
<?php echo $html->submit('View Person')?>
</form>
In order for this to work you need to have 'Ajax' in your helpers
array, and have the Prototype/script.aculo.us libraries included.
Good luck.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With