Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best library to create an AJAX auto-suggest textbox in a web form?

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?

like image 594
Chris Thompson Avatar asked Oct 20 '08 22:10

Chris Thompson


1 Answers

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.

like image 122
Paolo Bergantino Avatar answered Sep 18 '22 23:09

Paolo Bergantino