cakephp autocomplete - 传递参数

时间:2011-03-15 22:03:36

标签: ajax cakephp

我目前有这个代码。我正在尝试根据下拉列表中的值执行自动完成功能。问题是我不知道如何在ajax请求的下拉列表中传递所选值...

使用普通香草scriptaculous和原型

  <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>

2 个答案:

答案 0 :(得分:0)

如果您正在搜索页面,我建议使用GET而不是POST传递参数。用户通常会将搜索标记为书签,如果使用POST完成,则无法保留结果。

这是一个如何进行自动完成的(快速黑客攻击)示例。

您的搜索视图

<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>

它需要一个名为'controller'的控制器,它使用两个函数('search'和'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');      
}

autocomplete ajax piece需要一个名为'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>

这应该让你指向正确的方向。

(如果你想要的是获取select标签值并通过ajax传递它,那么你应该看看用于序列化表单的ajax函数。你可能不得不手工编写javascript代码,因为我没有知道ajax帮助器是否有序列化表单的选项。这里有一些代码我用来序列化只是一个select标签。可能会有所帮助)

<?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')"
            )
        )
    );
?>

答案 1 :(得分:0)

我明白了。您希望序列化'searchBy'参数,以便在自动完成功能中使用它。 If you check out the documentation, there is an option for adding additional parameters.

有一个用于序列化表单的原型函数。我将它用于常规表单,但我从未用于自动填充。试一试。

<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>