3

I want to create search form and then show result by view. After i submit the result show in view searchome but when refresh page after submit i get re submission how can i search by post route and prevent re submission by refresh page?

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);
}

My search view

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

My searchome view

<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(); ?>

My Route

Route::post('findsresult', 'SearchController@src_fn')->name('findsresult');
The Rock
  • 393
  • 3
  • 12
  • 29
  • It is browser default! better use get if u dont want browser to show `confirm form resubmission` – arun Jan 02 '18 at 08:36
  • @arunkumar yes i see but when i use get my url so messy – The Rock Jan 02 '18 at 08:39
  • May be this is helpful to your question: https://stackoverflow.com/questions/6320113/how-to-prevent-form-resubmission-when-page-is-refreshed-f5-ctrlr – vaibhav raychura Jan 02 '18 at 08:54
  • IMHO, for searching and filtering using `GET` is best solution. If your search feature is for public user it will indexed by `search engine`. This is not posible if you use `POST`. – Dharma Saputra Jan 02 '18 at 09:00
  • You can add a get route for showing the results then in the `src_fn` method redirect to that route to distroy the post request ! – Maraboc Jan 02 '18 at 09:26
  • @Maraboc can you create answer – The Rock Jan 02 '18 at 09:53

1 Answers1

2

To distroy the post request and prevent the browser to resend it over and over after refreshing you can use Post/Redirect/Get design pattern, to do that you can create a new get route :

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

And in the src_fn of the controller call the new get route using a redirect :

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

And in the src_show_fn method :

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);
}
Maraboc
  • 10,550
  • 3
  • 37
  • 48