3
votes

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');
1
It is browser default! better use get if u dont want browser to show confirm form resubmissionarun
@arunkumar yes i see but when i use get my url so messyThe Rock
May be this is helpful to your question: stackoverflow.com/questions/6320113/…vaibhav raychura
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
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

1 Answers

2
votes

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