views:

163

answers:

2

I'm setting up jQuery UI autocomplete on a CakePHP site, and feel like I'm missing something really obvious, but it's almost working...

So the simple jQuery code is:

$(function() {
    $("#SearchQuery").autocomplete({
        source: "<?= $session->base ?>/search/complete",
        minLength: 2
    });
});

This connects to my search controller, and the search controller returns items from the database. I can get the results back if I don't try to actually look at the "term" (what has been typed. So, in my controller this works:

function complete(){
    $entries = $this->Entry->find('list');
    $this->set('entries', $entries);
}

It is returned as JSON, and drops down from the input like it should. But it never narrows the results based on what has been typed. I would think that this should narrow the results:

function complete($query = null){
    $entries = $this->Entry->find('list', 
        array('conditions' => array('title LIKE' => '%'.$query.'%')));
    $this->set('entries', $entries);
}

But with this code nothing is ever returned. What am I doing wrong?

P.S. People who find this question and are having trouble getting the results back to the autocomplete function, be sure you're returning properly formatted JSON results: http://www.pagebakers.nl/2007/06/05/using-json-in-cakephp-12/

A: 

The answer is pretty simple, as expected... "term" is passed as $_GET['term'], so it's not available from the controller function's variables. (Why I thought it would be passed as a URL segment I have no idea...)

This works:

function complete(){
    $query = $_GET['term'];

    $entries = $this->Entry->find('list', 
        array('conditions' => array('title LIKE' => '%'.$query.'%')));
    $this->set('entries', $entries);
}
handsofaten
It's available. use: $this->params['url']['term']; :-)
Nik
yeah. this is one of those cases where i'm not sure what the advantage is of using `$this->params['url']` vs. `$_GET`. is it safer somehow?
handsofaten
If it's just a query for a drop down, I don't think security is too much of an issue. If it concerns you, though, you can clean up the term with standard php before you act on it.
Leo
A: 

Hello, I am trying to get jQuery UI Autocomplete running with CakePHP too. But in my case, simply nothing happens when i start typing.

jQuery und jQuery UI are included correctly This works for example...

 $(function() {
   var availableTags = [
   ...
   ];
   $( "#tags" ).autocomplete({
   source: availableTags
 });
 }); 

But if I try to get some data from a controller method it won't.

<script>
$(document).ready(function() {
  $( "#query" ).autocomplete({
  source: "<?= $session->base ?>/verses/autoComplete"
  });
});
</script>


<input type="text" id="query" value="" onClick=""/>

It seems the function does not find the method?!

Can you help? Or post full example?

Thanks in advance...!!!

function autoComplete() {
  $this->layout = 'ajax';
  echo $_GET['term'];
}
Fabian Petrik
Look at the `complete()` function in my answer. You need to get the search term with either `$_GET['term']` or `$params['url']['term']`, and do a search for that term in your database, using `$this->Model->find()`. Then return the results. But, this is very important, the results have to be returned as JSON. This is a handy tutorial on setting up your layout / view to return JSON properly: http://www.pagebakers.nl/2007/06/05/using-json-in-cakephp-12/
handsofaten