php - 如果字段失败,提交后在 laravel 中动态保持数据表单
问题描述
提交和请求失败后如何在 Laravel 7 中动态保存所有数据。单击(按钮添加更多)后动态创建的所有表单被删除仅保留第一个表单并批量所有旧数据我的问题。
1-如何保持所有表单动态创建
2 - 如何保留所有数据
刀锋形态
<!DOCTYPE html>
<html>
<head>
<title>Add/remove multiple input fields dynamically</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
<div class="container">
<h2 align="center"> </h2>
<form action="{{ route('addmorePost') }}" method="POST">
@csrf
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@if (Session::has('success'))
<div class="alert alert-success text-center">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<p>{{ Session::get('success') }}</p>
</div>
@endif
<table class="table table-bordered" id="dynamicTable">
<tr>
<th>Name</th>
<th>Qty</th>
<th>Price</th>
<th>Action</th>
</tr>
<tr>
<td><input type="text" name="addmore[0][name]" placeholder="Enter your Name" class="form-control" /></td>
<td><input type="text" name="addmore[0][qty]" placeholder="Enter your Qty" class="form-control" /></td>
<td><input type="text" name="addmore[0][price]" placeholder="Enter your Price" class="form-control" /></td>
<td><button type="button" name="add" id="add" class="btn btn-success">Add More</button></td>
</tr>
</table> <button type="submit" class="btn btn-success">Save</button>
</form>
</div>
<script type="text/javascript">
var i = 0;
$("#add").click(function(){
++i;
$("#dynamicTable").append('<tr><td><input type="text" name="addmore['+i+'][name]" placeholder="Enter your Name" class="form-control" /></td><td><input type="text" name="addmore['+i+'][qty]" placeholder="Enter your Qty" class="form-control" /></td><td><input type="text" name="addmore['+i+'][price]" placeholder="Enter your Price" class="form-control" /></td><td><button type="button" class="btn btn-danger remove-tr">Remove</button></td></tr>');
});
$(document).on('click', '.remove-tr', function(){
$(this).parents('tr').remove();
});
</script>
</body>
</html>
控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\ProductStock;
class ProductAddMoreController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function addMore()
{
return view("addMore");
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function addMorePost(Request $request)
{
$request->validate([
'addmore.*.name' => 'required',
'addmore.*.qty' => 'required',
'addmore.*.price' => 'required',
]);
foreach ($request->addmore as $key => $value) {
ProductStock::create($value);
}
return back()->with('success', 'Record Created Successfully.');
}
}
解决方案
如果请求失败,您可以在发布请求期间将请求输入刷新到会话中,然后如果您刷新的数据在那里,则检查另一条路由,然后根据旧输入构建表的一些逻辑https://laravel.com/ docs/8.x/requests#flashing-input-to-the-session
或者,如果您的懒惰,您可以在提交表单时发出 XHR 请求,仅用于验证检查,然后仅在通过时提交
推荐阅读
- javascript - Javascript函数绘制
- javascript - 谷歌可视化多个图表 - 选项不起作用
- node.js - Node JS使用文件夹中的所有文件
- scala - 使用 scastie 和 scalafiddle 评估代码执行时间
- r - 如果一个数据集是因子类型,另一个是 R 中的日期类型,如何合并两个数据集?
- python - Pipenv 无法识别
- asp.net - 下载我已经创建 ASP.Net 的 PDF
- vb.net - List(Of T) 按 T 属性排序
- matlab - 让它识别关系的错误在哪里?
- python-3.x - 当有多个匹配项时使用 min() 时 Python 获取随机值