views:

1588

answers:

3

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?

+2  A: 

I've had great success with Brand Spanking New's Auto-Suggest implementation. It includes PHP examples too.

yalestar
Upvote solely for your avatar.
MrBoJangles
+1  A: 

You can't go wrong with jQuery. http://nodstrum.com/2007/09/19/autocompleter/

Craig
+5  A: 

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.

Paolo Bergantino