在octobercms中单击“提交”时,如何使用表中的其他用户输入字段数据保存多行数据?

问题描述

我有一个带有输入字段的表,使用foreach循环从数据库中加载数据,如下图所示。用户可以输入要为每个细分提取的金额。我有一个提交按钮

enter image description here

如果用户为任何行输入金额,我想获取表中每一行的信息(id,余额和输入金额)并将其提交到数据库中。

我如何获得它们?

 {{ form_ajax('onWithdrawal',{id:"form",class: 'form-horizontal','data-request-files':true,'data-request-flash': true}) }}
 <div id="container" class="table-responsive kv-grid-container">
     <table class="kv-grid-table table table-striped kv-table-wrap">
          <thead>
             <tr>
                <th>{{ 'Sub Divisions'|_ }}</th>
                <th>{{ 'Balance'|_ }}</th>
                <th>{{ 'Amount Taken'|_ }}</th>
                <th>{{ 'Withdraw'|_ }}</th>
            </tr>
          </thead>
          <tbody>
                            
                            
             {% for log in logs %}
                              
                 <tr id = "{{log.id }}">
                 <td>{{log.name }}</td> 
                 <td>{{log.balance }}</td>
                 <td>{{log.withdraw }}</td>
                 <td><input type="number" min=1000  max="1500" id="item-b"class="form-control" name="amount" placeholder="1,000" ></td>
                                       
              </tr>
            {% endfor %}
                            
         </tbody>
    </table>
                        
                        
    <div class="form-group">
        <button type="submit" class="btn btn-default "  data-request="onWithdrawal" data-request-files  style="float:right"><span class="glyphicon glyphicon-refresh"></span>{{ 'Submit'|_ }}
         </button>
                               
    </div>
</div>

{{ form_close() }}

PHP部分

function onWithdrawal(){
  $user = Auth::getUser();

               $model = new WithdrawLog();
                $model->user_id = $user->id;
                $model->email = $user->email;
                $model->id = ?
                $model->name = ?
                $model->amount = Input::get("amount");
    

          if ($model->save()) {
                         Flash::success('Withdrawal Application successfully sent');  
                         return Redirect::refresh();
                    } else {
                        Flash::error('error');  
                        return Redirect::refresh();
                    }
                    
    
    } 
}

解决方法

有很多方法可以做您正在做的事情。如果您正在寻找安全的东西,则应该考虑对行进行验证。

第一件事是将您的表单更改为标准HTML表单。调试起来更容易。

{#{{ form_ajax('onWithdrawal',{id: 'form',class: 'form-horizontal','data-request-files':true,'data-request-flash': true}) }}#}
{{ form_open({request: 'onWithdrawal',id:'form',files: 'true'}) }}

在您的HTML中添加一些隐藏的元素,并按commonId[nameValue]对您的名字进行分组。 Learn more here

<td><input type="hidden" name="{{ log.id }}[id]" value="{{ log.id }}"><input type="number" min=1000  max="1500" id="item-b"class="form-control" name="{{ log.id }}[amount]" placeholder="1,000" ></td>

现在您的PHP可以如下所示:

function onWithdrawal(){
    foreach(Input::except('_session_key','_token') as $row) {

        $user = Auth::getUser();

        $model = WithdrawLog::find($row['id']); //If they exist you only need to find them.
        $model->user_id = $user->id;
        $model->email = $user->email;
        $model->id = $row['id'];
        $model->amount = $row['amount'];

        $model->save();
        
    }
    // Should prolly look into validating or checking which records were not updated.
    Flash::success('Withdrawal Application successfully sent');  
    return Redirect::refresh();
} 
,

我这样解决了。

在HTML

<td><input type="number" min=1000  max="1500" id="item-b"class="form-control" name="amount[{{ log.id }}]" placeholder="1,000" ></td>

对于PHP,我做了类似的事情。

function onWithdrawal(){
  $user = Auth::getUser();

               $model = new WithdrawLog();
                $model->user_id = $user->id;
                $model->email = $user->email;

         foreach(post("amount") as $id => $amount){
            if($amount !=""){
                $model->id = $id;
                $model->amount = $amount;
              }
         }

          if ($model->save()) {
                         Flash::success('successfully sent');  
                         return Redirect::refresh();
                    } else {
                        Flash::error('error');  
                        return Redirect::refresh();
                    }
                    
    
    } 
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...