【问题标题】:Multiple forms, one submit button in cakephp 3多个表单,cakephp 3 中的一个提交按钮
【发布时间】:2016-07-21 17:00:24
【问题描述】:

我在使用 cakephp 3 的应用程序中工作,在这个应用程序中,管理员用户可以注册为其他用户支付的费用,我想要做的是允许用户在同一视图中一次创建多个支付,每次支付一次用户。我所做的是首先为每个用户创建一个重复字段的表单。

问题是当我单击提交按钮(我只想要一个)时,我在控制器中接收的数据始终是最后一个字段的数据。我不知道如何正确处理。

mi view的代码如下:

addMulti.ctp

<div class="pagos form col-lg-offset-2 col-lg-7">
<?= $this->Form->create(false) ?>
<?php foreach ($users as $user):
        echo '<br><strong>'.$user->full_name.'</strong>';
        echo $this->Form->checkbox('selected',['id'=>'select-'.$user->id,'onclick'=>'display_add_form('.$user->id.')']).'<br>';
        echo '<div style="display: none;" id= form-'.$user->id.'>';
        echo $this->Form->input('user_id', array(
            'type' => 'hidden',
            'value' => $user->id,
            'id'=>'user-'.$user->id
        ));
        echo $this->Form->input('mes', ['type' => 'month','selected'=>'0','id'=>'month-'.$user->id]);
        echo $this->Form->input('monto',['default' => $user->monto_paga,'id'=>'monto-'.$user->id]);
        echo $this->Form->input('año', ['type' => 'year', [
            'minYear' => date('Y')-2,
            'maxYear' => date('Y')+2
        ],'value'=> date('Y'),'id'=>'year-'.$user->id]);
        echo $this->Form->input('forma_pago', ['options'=>['Efectivo'=>'Efectivo','Transferencia'=>'Transferencia','Cheque'=>'Cheque'],'id'=>'pago-'.$user->id]);
        echo $this->Form->button('Cancelar',['type'=>'button','onclick'=>'hide_add_form('.$user->id.')','class'=>'btn btn-danger','id'=>'cancel-'.$user->id]);
        echo '</div>';
    endforeach;
    echo $this->Form->button(__('Agregar Pagos'),['class'=>'btn btn-primary']);
    echo $this->Form->end(); ?>

<script>
function display_add_form(id){
    $("#form-"+ id).show('fast');
    $("#select-"+ id).hide();
}
function hide_add_form(id) {
    $("#form-"+ id).hide('fast');
    $("#select-"+ id).show();
}

【问题讨论】:

    标签: php forms cakephp cakephp-3.0


    【解决方案1】:

    要继续@Asif 的答案,如果您分别保存每个用户会更容易。

    ...
    echo $this->Form->input('users['.$user->id.'][mes]', ['type' => 'month','selected'=>'0','id'=>'month-'.$user->id]);
    echo $this->Form->input('users['.$user->id.'][monto]',['default' => $user->monto_paga,'id'=>'monto-'.$user->id]);
    ...
    

    通过这种方式,您可以在后端使用简单的循环,例如:

    <?php
    foreach($input['users'] as $userId => $user){
        echo $user['mes'];
    }
    

    【讨论】:

      【解决方案2】:

      您可以将归档名称作为数组,这样您就可以在数组中获取所有表单 POST 值,因为您在单个变量下发布多个值。我只是在下面给出一个简单的 html 输入字段示例。希望你知道如何添加 cakephp。

        <input type="text" name="year[]" id="your-id">
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-23
        • 2015-10-04
        • 2011-03-20
        • 2017-05-03
        相关资源
        最近更新 更多