在cakephp 3中有多个表单,一个提交按钮

时间:2016-07-21 17:00:25

标签: php forms cakephp cakephp-3.0

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

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

以下是mi视图的代码:

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

2 个答案:

答案 0 :(得分:2)

要继续@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'];
}

答案 1 :(得分:1)

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

  <input type="text" name="year[]" id="your-id">
相关问题