搜索cakephp中的所有控制器

时间:2014-09-16 11:23:55

标签: javascript forms cakephp controller

我需要从单个表单对任何控制器执行查询,我试试这个:

<select id="control">
        <option value="labs">Laboratórios</option>
        <option value="computers">Computadores</option>
        <option value="hists">Históricos</option>
    </select>
    <script>
        $(document).ready(function(){
            $("#control").change(function(){
                var value = $(this).val();
            });
        });
    </script>
    <?php 
        $controller = '<script>document.write(value);</script>';
        print_r($controller);
        $base_url = array('controller' => $controller, 'action' => 'index' );
        echo $this->Form->create("Filter",array('url' => $base_url, 'class' => 'filter'));
        echo $this->Form->input("search", array('label' => 'Pesquisa', 'placeholder' => "Pesquisa..."));
        echo $this->Form->submit("Pesquisar");
        echo $this->Html->link("Reset",$base_url);
        echo $this->Form->end(); 
    ?>

但变量$ controller有字符串&#34; <script>document.write(value);</script>&#34;,如何解决这个问题?或者有另一种方法来做到这一点......

2 个答案:

答案 0 :(得分:0)

尝试model代替controller -

<?php 
    $base_url = array('controller' => 'any_controller', 'action' => 'any_action' );
    echo $this->Form->create("Filter",array('url' => $base_url, 'class' => 'filter'));
    $models = array('Model1' => 'Model1', 'Model2' => 'Model2', 'Model3' => 'Model3');
    echo $this->Form->input("model", array('label' => 'Model', 'options' => $models));
    echo $this->Form->input("search", array('label' => 'Pesquisa', 'placeholder' => "Pesquisa..."));
    echo $this->Form->submit("Pesquisar");
    echo $this->Html->link("Reset",$base_url);
    echo $this->Form->end(); 
?>

并且在`any_controller的any_action()中,你将获得所有的价值。

function any_action() {
    $model = $this->request->data['Filter']['model'];
    //instantiate the model
    $modelInit = ClassRegistry::init($model);
    //build search conditions depending on the other data
    $condition = array(...);
    $result = $modelInit->find('all', $condition);
    //set the result and redirect to your page`
}

答案 1 :(得分:0)

您可以在更改下拉列表时创建表单操作。我认为你不需要PHP。

<select id="control">
    <option value="labs">Laboratórios</option>
    <option value="computers">Computadores</option>
    <option value="hists">Históricos</option>
</select>
<script>
    $(document).ready(function(){
        //This will change the action on change of dropdown value
        $("#control").change(function(){
            var hostname = window.location.hostname;
            var controller = $(this).val();
            $('#filterForm').attr('action', 'http://'+hostname+'/'+controller+'/index');
        });
    });
</script>
<?php         
    $base_url = array('controller' => $controller, 'action' => 'index' );
    echo $this->Form->create("Filter",array('url' => $base_url, 'class' => 'filter','id'=>'filterForm'));
    echo $this->Form->input("search", array('label' => 'Pesquisa', 'placeholder' => "Pesquisa..."));
    echo $this->Form->submit("Pesquisar");
    echo $this->Html->link("Reset",$base_url);
    echo $this->Form->end(); 
?>

希望这对你有用..