[英]Form post request return error 419 unknown status laravel
I want to create new user with id_ward, but when I post submit form it returns error 419(unknown status) 我想用id_ward创建新用户,但是当我发布提交表单时,它返回错误419(未知状态)
please help me 请帮我
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalCenterTitle">Thêm user</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="./admin/users/add_user" method="post" id="form-add-user">
<input type="hidden" name="_token" value="{{csrf_token() }}" />
<div class="form-group row">
<div class="col-md-6">
<label for="add_user_area">Phường/xã:</label>
<select id="add_user_area" class="form-control" name="id_ward">
@foreach ($areas as $area)
<option value={{$area->id}}>{{$area->name}}</option>
@endforeach
</select>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" onclick="document.getElementById('form-add-user').submit()"
class="btn btn-primary">Thêm
</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Đóng</button>
</div>
</div>
And this is my controller 这是我的控制器
function postAddUser(Request $request)
{
$user = new User;
$user->id_ward = $request->id_ward;
$user->save();
return redirect('admin/users/list_user');
}
If you inspect your form using devtools does your token has any value at all? 如果您使用devtools检查表单,那么令牌是否具有任何价值? or does it have an empty value?
还是有一个空值?
Try adding this line request()->session()->regenerateToken();
尝试添加此行
request()->session()->regenerateToken();
to your controller before you show view containing the form: 在显示包含表单的视图之前,请先将其连接到控制器:
public function showAddUserForm()
{
request()->session()->regenerateToken();
return view('user.create');
}
I had the same issue because I was trying to add a modal with a form to a page that already has a form. 我遇到了同样的问题,因为我试图向带有表单的页面添加带有表单的模式。 To solve it I gave my forms unique ids and then with the help of the form attribute added the form id to all my inputs including the csrf input.
为了解决这个问题,我给了表单唯一的ID,然后在form属性的帮助下,将表单ID添加到了所有输入中,包括csrf输入。 Below is an example.
下面是一个例子。 I am hopeful it would solve your problem
我希望它能解决您的问题
<form action="{{route('template.update')}}" id="edit_template_{{$template->id}}" method="post">
<input type="hidden" name="_token" value="{{csrf_token() }}" form="edit_template_{{$template->id}}">
<input type="number" name = "unit_price" form="edit_template_{{$template->id}}">
<button form="edit_template_{{$template->id}}">Save</button>
</form>
Try replacing 尝试更换
<input type="hidden" name="_token" value="{{csrf_token() }}" />
-> ->
@csrf
It will help https://laravel.com/docs/5.8/csrf 它将帮助https://laravel.com/docs/5.8/csrf
If that doesn't help Let's understand. 如果那没有帮助,请让我们理解。 What we have written in routes?
我们在路线中写了什么? There must be something like.
一定有类似的东西。
Route::post('/users/add_user', 'UsersController@postAddUser')->name('postAddUser');
in controller 在控制器中
public function postAddUser() {
$user = new User();
$user->id_ward = request()->id_ward;
$user->save();
return redirect('admin/users/list_user');
}
in view 鉴于
<form action="{{ route("postAddUser") }}" method="post" id="form-add-user">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.