2

I have a search bar in view:

{!! Form::open(['name' => 'myForm', 
                'method' => 'GET', 
                'action' => 'AreaController@search',
                'files' => true, 
                'onsubmit' => "return validateForm()"]) 
!!}
         
    {!! Form::submit('جستجو', ['class' => 'btn btn-info']) !!}
                       
{!! Form::close() !!}

And in route file, web.php:

Route::get('area/search/', 'AreaController@search')->name('area.search');

AreaController:

public function search(Request $request) {
    return " it is working" ;
}

But when I click on the button, the browser is showing a blank page. When I use POST method it is working, but if I change to GET method, it is not working.

Thank you.

H H
  • 2,065
  • 1
  • 24
  • 30
Pooya Chavoshi
  • 346
  • 4
  • 12
  • I suppose this is a LaravelCollective form? Not sure your route is hit at all when using `'action'=>'AreaController@search'`. Also, your post is set to accept file uploads `'files'=>true` ... so your method must be POST – brombeer Apr 04 '21 at 15:11
  • i removed 'files'=>true and instead of 'action'=>'AreaController@search' used 'route' => ['area.search'] but still not working – Pooya Chavoshi Apr 04 '21 at 15:17
  • when I changed route from area/search/ to area-search. it worked !!! – Pooya Chavoshi Apr 04 '21 at 16:37

1 Answers1

0

For Post Method, you need to add CSRF Token To A Form


{!! Form::open(['method' => 'POST']) !!}  <--------- Change to POST method
    
{!! Form::token() !!}  <----------- Add this line 


    {!! Form::submit('جستجو', ['class' => 'btn btn-info']) !!}
                       
{!! Form::close() !!}


Route::post('area/search/', 'areacontroller@search')->name('area.search');

OR

If you don't want to add CSRF Token To A Form, You can add that to the route file.

Attaching The CSRF Filter To A Route


Route::post('profile', array('before' => 'csrf', function()
{
    //
}));

For More Details, you can refer to https://laravel.com/docs/4.2/html

Yudiz Solutions
  • 4,216
  • 2
  • 7
  • 21