【问题标题】:Cakephp form has to be submitted twice to workCakephp 表单必须提交两次才能工作
【发布时间】:2013-08-22 21:35:37
【问题描述】:

我正在尝试解决我无法理解的表单提交问题。当我第一次提交表单时,在更改下拉列表的值后,$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-&gt;request-&gt;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")
        }
    });
});

希望有帮助

【问题讨论】:

  • 不知道,出了什么问题。但只需尝试从表单输入选项中删除 'selected' 属性,因为它在 cakephp2.0 中已被弃用。你可以查看here
  • 请写你的js代码
  • 我已经编辑了我的原始问题并添加了用于此表单的 JS。
  • 我尝试了我的表单的测试版本,删除了 'selected' 属性,但没有任何区别。我意识到 'selected' 属性在 2.0 中已被弃用,但我找不到一个像样的参考来告诉我如何使用 $attributes['value'] 代替。尽管不推荐使用“selected”,但 cakephp 2 的许多参考似乎仍在使用“selected”选项
  • 是否还有其他可能正在运行的 JS 代码? AppController 中的请求有什么问题吗?使用浏览器的控制台查看您的请求是否第一次正确发布。如果是,则问题出在服务器端,否则是客户端。

标签: php javascript jquery forms cakephp


【解决方案1】:

我注意到两件事:

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;
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    • 2020-06-29
    • 2013-03-18
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多