在 octobercms 中单击提交时,如何使用来自 table 的附加用户输入字段数据保存多行数据?
How to save multiple rows of data with additional user input fields data from a table when click submit in octobercms?
我有一个带有输入字段的 table,数据是使用 foreach 循环从数据库加载的,如下图所示。用户可以为每个细分输入他们想要提取的金额。我有一个提交按钮
如果用户为任何行输入金额,我想获取 table 中每一行的信息(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', class: 'form-horizontal', 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();
}
}
}
我有一个带有输入字段的 table,数据是使用 foreach 循环从数据库加载的,如下图所示。用户可以为每个细分输入他们想要提取的金额。我有一个提交按钮
如果用户为任何行输入金额,我想获取 table 中每一行的信息(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', class: 'form-horizontal', 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();
}
}
}