我有一张表格,该表格在成功验证后应显示期望的结果。但是在按下按钮时,浏览器显示该页面由于不活动而过期。请刷新,然后重试。
查看页面
<form class="form-horizontal" method="POST" action="{{action('BookController@store')}}">
<div class="row" style="padding-left: 1%;">
<div class="col-md-4">
<div class="form-group">
<label>Book Name</label><span class="required">*</span>
<input type="text" maxlength="100" minlength="3" required="required" runat="server" id="txtBookName" class="form-control" autocomplete="off" autofocus="autofocus" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</div>
</form>
路线代码
//web.php
Route::resource('book','BookController');
控制器代码
class BookController extends Controller
{
public function index()
{
//
}
public function create()
{
return view('pages.book');
}
public function store(Request $request)
{
$validatedInput = $request -> validate([
'txtBookName' => 'required|string|min:3|max:100'
]);
return $validatedInput;
}
}
表格网址 http://127.0.0.1:8000/book/create
在按下提交按钮时,页面被重定向到
http://127.0.0.1:8000/book并显示The page has expired due to inactivity. Please refresh and try again.
答案 0 :(得分:2)
您可以通过以下方式在表单中发布CSRF令牌:
{{ csrf_field() }}
或者在app / Http / Middleware / VerifyCsrfToken.php中排除您的路线:
protected $except = [
'your/route'
];
答案 1 :(得分:1)
像这样实施以避免过期消息。
<form action="{{ action('ContactController@store') }}" method="POST">
@csrf <!-- this is the magic line, works on laravel 8 -->
<!--input components-->
<!--input components-->
<!--input components-->
</form>
答案 2 :(得分:0)
您应在表单中使用{{csrf_field()}}。
答案 3 :(得分:0)
请在表单类之后执行此操作,因为资源使用@method('PUT')
<form class="form-horizontal" method="POST" action="{{action('BookController@store')}}">
{{csrf_field()}}
@method('PUT')
<div class="row" style="padding-left: 1%;">
<div class="col-md-4">
<div class="form-group">
<label>Book Name</label><span class="required">*</span>
<input type="text" maxlength="100" minlength="3" required="required" runat="server" id="txtBookName" class="form-control" autocomplete="off" autofocus="autofocus" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</div>
</form>