2

我正在尝试解决我无法理解的表单提交问题。当我第一次提交表单时,更改下拉列表的值后,$this->request->data数组为空。如果我再次提交,我会看到我所期望的。每次我更改表单上的任一下拉菜单时都会发生这种情况。

这是表格:

<?php

echo $this->Form->create('Refine', array('url' => '/ServiceDirectoryResults/refine'));

echo $this->Form->input('state', array(
    'type' => 'select',
    'label' => 'State',
    'options' =>  $all_states,
    'selected' => array('state_selected', $state_selected),
    'id' => 'state',
    ));

echo $this->Form->input('solution', array(
    'type' => 'select',
    'label' => 'Solution',
    'options' => $solutions,
    'selected' => array('selected', $solution),
    'id' => 'solutions',
    ));

echo $this->Form->input('region', array(
    'before' => '<fieldset id="Region">',
    'multiple' => 'checkbox',
    'options' => $regions,
    'selected' => $reg_selected,
    'after' => '</fieldset>'
    ));

echo $this->Form->input('tags', array(
    'before' => '<fieldset id="TagBox">',
    'multiple' => 'checkbox',
    'options' => $narrow,
    'selected' => $tag_selected,
    'after' => '</fieldset>'
    ));

echo $this->Form->end('Refine Search');

?>

表格渲染得很好。如果更改了状态或解决方案下拉列表并提交了表单,则$this->request->data数组为空。如果我第二次提交,而不更改任何内容,则数组包含我希望看到的内容。

在我的控制器中,我有

if(isset($this->request->data['Refine']['state']))
{
    $state = $this->request->data['Refine']['state'];
}

显然,如果数组为空,我在第一次提交表单时在状态变量中什么也得不到。

如果有人能对这种行为有所了解,我将不胜感激。我在创建表单时做错了什么?

这里要求的是与此表单一起使用的 js。这个想法是,如果“全部”复选框是为控制器中的区域和标签创建的第一个复选框,它只负责设置或清除复选框。

$(document).ready(function(){

    $("#RefineRegion0").click(function(){

        if ($("#Region #RefineRegion0").is(':checked')) {
            $("#Region input[type=checkbox]").each(function (e) {
                $(this).prop("checked", true);
            });

        } else {
            $("#Region input[type=checkbox]").each(function (e) {
                $(this).prop("checked", false);
            });
        }
    });

    $("#RefineTags0").click(function(){
        if ($("#TagBox #RefineTags0").is(':checked')) {
            $("#TagBox input[type=checkbox]").each(function (e) {
                $(this).prop("checked", true);
            });

        } else {
            $("#TagBox input[type=checkbox]").each(function (e) {
                $(this).prop("checked", false);
            });
        }
    });

    $("#RefineViewForm").submit(function(){
        if($('#state').val() == "" || $('#solutions').val() == ""){
            alert("Please select a State and Solution before continuing")
        }
    });
});

希望有帮助

4

1 回答 1

0

我注意到两件事:

1) 表单的 url:控制器名称为小写并带有下划线:service_directory_results。请参阅 cakephp 名​​称约定:http: //book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html。但我认为最好使用数组作为 url,这样你的路由就可以匹配:

echo $this->Form->create('Refine', array('url' => array('controller' => 'service_directory_results', 'action' => 'refine')));

2)如果这些字段为空,则在您的 Js 上不要发送添加返回 false 的帖子;(也缺少一个;)

$("#RefineViewForm").submit(function(){
    if($('#state').val() == "" || $('#solutions').val() == ""){
        alert("Please select a State and Solution before continuing");
        return false;
    }
});
于 2013-11-21T12:04:47.030 回答