Form post request return error 419 unknown status laravel

纵然是瞬间 提交于 2019-12-31 04:36:08

问题


I want to create new user with id_ward, but when I post submit form it returns error 419(unknown status)

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">&times;</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');
}

回答1:


If you inspect your form using devtools does your token has any value at all? or does it have an empty value?

Try adding this line request()->session()->regenerateToken(); to your controller before you show view containing the form:

public function showAddUserForm()
{
    request()->session()->regenerateToken();
    return view('user.create');
}



回答2:


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. 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>



回答3:


Try replacing

<input type="hidden" name="_token" value="{{csrf_token() }}" />

->

@csrf

It will help 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">


来源:https://stackoverflow.com/questions/55160918/form-post-request-return-error-419-unknown-status-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!