简体   繁体   English

laravel中的表单提交无法正常工作,并显示令牌不匹配异常

[英]Form submission in laravel is not working and show token-mismatch Exception

i'm new laravel learner. 我是新的Laravel学习者。 i have create a form in laravel 5.4 and add two field one is textfield and another is submit button. 我已经在Laravel 5.4中创建了一个表单,并添加了两个字段,一个是textfield,另一个是Submit按钮。 i have create a name route for form submission. 我已经为表单提交创建了一个名称路由。 but when i submit the form it's show token mismatch exception. 但是当我提交表单时,它显示令牌不匹配异常。 i can't find out error from my code. 我从我的代码中找不到错误。 Here, is my form code. 这是我的表单代码。

<form method="post" action="{{route('f.submit')}}">
    {{csrf_field()}}

    <input class="form-control" type="text" name="fname">
    <input type="submit" name="submit" value="Create">
</form>

here is my route, 这是我的路线

Route::post('formsubmit','HomeController@formSubmit')->name('f.submit');

and here my controller, 还有我的控制器

 public function formSubmit()
{
    echo "Form Submitted";
}

As of Laravel 5.6 you can just use the new blade directive of @csrf . Laravel 5.6开始,您可以使用新的@csrf刀片指令。 In example: 例如:

<form method="POST" action="/profile">
    @csrf
    ...
</form>

You have to add input hidden field for token. 您必须为令牌添加输入隐藏字段。 change you view as: 将您的视图更改为:

<form method="post" action="{{route('f.submit')}}">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">

    <input class="form-control" type="text" name="fname">
    <input type="submit" name="submit" value="Create">
</form>

Can you please make sure your /storage directory has proper write permissions (0755) ? 您能否确保/storage目录具有适当的写权限(0755)? If you're in Linux, you can change that by sudo chmod 0755 -R storage/ 如果您使用的是Linux,则可以通过sudo chmod 0755 -R storage/

Also, try not escaping the csrf_field() value. 另外,请尝试不要转义csrf_field()值。 Instead of {{ csrf_field() }} try {!! csrf_field !!} 代替{{ csrf_field() }}尝试{!! csrf_field !!} {!! csrf_field !!} since that helper function spits out HTML {!! csrf_field !!}因为该辅助函数吐出HTML

Try this. 尝试这个。 for Form. 用于表格。

<form method="post" action="{{route('f.submit')}}">
{{csrf_field()}}

<input class="form-control" type="text" name="fname">
<input type="submit" name="submit" value="Create">

In Controller. 在控制器中。

public function formSubmit(Request $request)
{
  $request->all();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM