cakephp autocomplete - pass a parameter
I currently have this code. I am trying to do an autocomplete based on a value in the dropdown. Problem is Im not sure how I can pass the selected value 开发者_开发问答in the dropdown in the ajax request...
Using plain vanilla scriptaculous and prototype
<form>
<?php echo $this->Form->input('searchby', array('type' => 'select', 'options' => $searchByList, 'label' => 'Search By:'));?>
</b>
<?php echo $ajax->autoComplete('searchvalue', '/Controller/autoComplete/', array('minChars' => 2)); ?>
<input name="search" type="submit" value="Search"/>
</form>
If you are doing a search page, I recommend passing the parameters using GET instead of POST. Often times users bookmark their searches, if its done using a POST, then the results cannot be preserved.
Here is a (quickly hacked together) example of how to do an auto-complete.
Your search view
<form method="get" action="/controller/search">
<?php echo $this->Form->input('Search.searchby', array('type' => 'select', 'options' => $searchByList, 'label' => 'Search By:'));?>
<?php echo $ajax->autoComplete('Search.searchvalue', '/controller/autoComplete/', array('minChars' => 2)); ?>
<input name="search" type="submit" value="Search"/>
</form>
It needs a controller named 'controller' that uses two functions ('search' and 'autocomplete')
function search() {
// GET parameters for search
$searchby = (isset($this->params['url']['searchby'])) ? ( $this->params['url']['searchby']) : '';
$searchvalue = (isset($this->params['url']['searchvalue'])) ? ( $this->params['url']['searchvalue']) : '';
// DO SEARCH HERE
$conditions = array('searchby'=>$searchby, 'searchvalue' => $searchvalue);
…etc…
}
function autocomplete() {
if(!empty($this->data['Search']['searchvalue'])){
$rs = $this->Search->findAll(array('searchvalue' => "LIKE {$this->data['Search']['searchvalue']}%" ));
$this->set('searchvalues', $rs);
}
$this->render('autocomplete', 'ajax');
}
The autocomplete ajax piece needs a view template named 'autocomplete.ctp'
<!-- autocomplete.ctp -->
<ul>
<?php if(isset($searchvalues) && !empty($searchvalues)){ ?>
<?php foreach($searchvalues as $t) { ?>
<li><?php echo $t['Search']['searchvalue']; ?></li>
<?php } ?>
<?php } ?>
</ul>
This should get you pointed in the right direction.
(If what you wanted was to get the select tag value and pass it via ajax, then you should take a look at the ajax functions for serializing the form. You may have to hand code the javascript, as I don't know if the ajax helper has options for serializing the form. here's some code I've used to serialize just a select tag. may be helpful)
<?php
$graph_type = array(
'impressions'=>'Impressions',
'click_throughs'=>'Click Throughs',
'click_through_ratio'=>'Click Through Ratio',
'time_with_brand'=>'Time with Brand',
'average_time_with_brand'=>'Average Time with Brand'
);
echo $html->selectTag('Graph/type', $graph_type, null, array('onChange' => 'event.returnValue = false; return false;', 'style' => 'margin-right:20px;'),null, false);
echo $javascript->event("'GraphType'", "change",
$ajax->remoteFunction(
array(
'update' => 'graph_container',
'url' => '/reports/ajax_graph_multiple',
'with'=>"Form.serialize('reports_form')"
)
)
);
?>
I see. You want the 'searchBy' parameter to be serialized so that it is available in your autocomplete function. If you check out the documentation, there is an option for adding additional parameters.
There is a prototype function for serializing forms. I use it for my regular forms, but I've never used for autocomplete. Give this a try.
<form id='autocomplete_form' name='autocomplete_form'>
<?php echo $this->Form->input('searchby', array('type' => 'select', 'options' => $searchByList, 'label' => 'Search By:'));?>
<?php echo $ajax->autoComplete('searchvalue', '/Controller/autoComplete/', array('minChars' => 2, 'parameters'=>"Form.serialize('autocomplete_form')")); ?>
<input name="search" type="submit" value="Search"/>
</form>
精彩评论