如果字段失败,则提交后在 laravel 中动态保留数据表单
keep data form dynamically in laravel after submit if have field fail
如何在提交和请求失败后在 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.');
}
}
如果请求失败,您可以在 post 请求期间将请求输入闪存到会话中,然后检查另一条路由是否存在您闪存的数据,然后使用一些逻辑来构建 table 基于旧输入 https://laravel.com/docs/8.x/requests#flashing-input-to-the-session
或者如果你很懒惰,你可以在提交表单时发出 XHR 请求以进行验证检查,然后仅在通过时才提交它
如何在提交和请求失败后在 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.');
}
}
如果请求失败,您可以在 post 请求期间将请求输入闪存到会话中,然后检查另一条路由是否存在您闪存的数据,然后使用一些逻辑来构建 table 基于旧输入 https://laravel.com/docs/8.x/requests#flashing-input-to-the-session
或者如果你很懒惰,你可以在提交表单时发出 XHR 请求以进行验证检查,然后仅在通过时才提交它