【问题标题】:Prevent resubmission post route in laravel防止在laravel中重新提交发布路线
【发布时间】:2018-01-02 08:13:22
【问题描述】:

我想创建搜索表单,然后按视图显示结果。在我提交结果显示在视图搜索组中但提交后刷新页面时我得到重新提交我如何通过发布路由搜索并防止通过刷新页面重新提交?

public function src_fn(){
  $para=Input::get('txt_pro_id');
  $result=DB::table('testing_tbl')->where('pro_id','=',$para)->paginate(5);
  return View::make('searchome')->with('result',$result);
}

我的搜索视图

<form method="post" action="{{route('findsresult')}}" name="frm_srch" role="search">
    {!! csrf_field() !!}
    <input type="Text" name="txt_pro_id" id="txt_pro_id">
    <input type="Submit" value="OK">
</form>

我的搜索视图

<table cellpadding="10px" width="100%" class="table_str">
    <thead style="font-size: 11px;">
        <tr>
            <th>Pro ID</th>
            <th>Pro Name</th>
            <th>Pro price</th>
        </tr>
    </thead>
    <tbody style="font-size: 11.5px;">
        @foreach($result as $vals)
            <tr scope="row" style="border-bottom: solid 1px #ccc">
                <td>{{$vals->pro_id}}</td>
                <td>{{$vals->pro_name}}</td>  
                <td>{{$vals->pro_price}}</td>                   
            </tr>
        @endforeach
    </tbody>
</table>
<?php echo $result->render(); ?>

我的路线

Route::post('findsresult', 'SearchController@src_fn')->name('findsresult');

【问题讨论】:

  • 浏览器默认!如果你不希望浏览器显示confirm form resubmission,最好使用 get
  • @arunkumar 是的,我明白了,但是当我使用时,我的网址变得如此混乱
  • 这可能对您的问题有帮助:stackoverflow.com/questions/6320113/…
  • 恕我直言,使用GET 进行搜索和过滤是最好的解决方案。如果您的搜索功能是针对公共用户的,它将由search engine 索引。如果您使用POST,这是不可能的。
  • 您可以添加一个 get 路由来显示结果,然后在 src_fn 方法中重定向到该路由以破坏发布请求!

标签: php laravel laravel-5 laravel-5.3


【解决方案1】:

要破坏发布请求并防止浏览器在刷新后一遍又一遍地重新发送,您可以使用Post/Redirect/Get 设计模式,为此您可以创建一个新的获取路由:

Route::get('showresults', 'SearchController@src_show_fn')->name('showresults');

并且在控制器的src_fn 中使用重定向调用新的get 路由:

public function src_fn(){
    $para=Input::get('txt_pro_id');
    return redirect('showresults')->with('pro_id', $para);
}

src_show_fn 方法中:

public function src_show_fn(){
    $para = session('pro_id');
    $result = DB::table('testing_tbl')->where('pro_id','=',$para)->paginate(5);
    return View::make('searchome')->with('result',$result);
}

【讨论】:

  • 但是当我刷新页面时没有发现数据意味着会话清除??
猜你喜欢
  • 2019-03-28
  • 2013-07-24
  • 1970-01-01
  • 2016-12-04
  • 2020-04-04
  • 2015-03-11
  • 2011-04-24
  • 2018-03-18
  • 2020-01-08
相关资源
最近更新 更多