【问题标题】:unable to post data through button无法通过按钮发布数据
【发布时间】:2020-09-02 15:12:01
【问题描述】:

我正在尝试通过按下按钮通过隐藏字段将数据发布到数据库 但我面临的错误是 419 Page Expired

这是我的看法:-

<div>
<h1> All Posts </h1>
@if(count($user) > 0)
  @foreach($user as $post)
<form action="/f" method="POST">
<input type="hidden" id="friends" name="friends" value="{{$post->id}}" />
<button type="submit" class="btn btn-primary">
                                    {{ __('chat') }}
                                </button>
</form>
<div class="well">
 <a href="/profile/1"<button class="button btn-success">chat</button>></a>
  <h3>{{$post->title}}</h3>
  <h4>{{$post->body}}</h4>

  <small>written on {{$post->created_at}}</small>
</div>

@endforeach
@else
   <p>No Forms Found</p>
@endif
</div>

这是我的路线:-

Route::post('/f', 'FriendsController@store');

这是我的控制器:-

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FriendsController extends Controller
{
    public function store(Request $request)
{   dd($request->all());
    post::create([
        'friends' => $request->friends,
        ]);
     return redirect('/profile/' . auth()->user()->id);
}
}

我可能在我的商店方法中犯了错误,请检查并告诉我它有什么问题 我只是一个初学者 -谢谢你

【问题讨论】:

标签: php html laravel forms controller


【解决方案1】:

错误 419 表示页面已过期,在 laravel 中,它来自于在发出非 GET 请求时没有或拥有无效的 CSRF 令牌。您需要在表单中添加 CRSF 令牌,例如,在表单声明之后添加 @csrf:

<form action="/f" method="POST">
@csrf
<input type="hidden" id="friends" name="friends" value="{{$post->id}}" />

或者这样:

<form action="/f" method="POST">
 {{csrf_field()}}
<input type="hidden" id="friends" name="friends" value="{{$post->id}}" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 2022-01-15
    • 2018-02-03
    • 1970-01-01
    相关资源
    最近更新 更多