Back

Submitting form gives error 419, session expired


I'm using Laravel 5.7

In my view, list.blade.php I have this (relevant portion shown)

<?php $counter=0; ?>

@foreach($items as $item)

<button type="button" class="addButton btn btn-default" onclick="document.getElementById({{$counter}}).submit()">

<i class="fa fa-close"></i>

<form id="{{$counter}}" action="{{ URL::route('list.remove') }}" method="POST" style="display: none;">

<input type="hidden" name="item" value="{{$item->item}}"></input>

@csrf
</form> <?php $counter++;?>

@endforeach

Relevant controller in web.php:

Route::post('/list/remove', '[email protected]')->name('list.remove')->middleware('auth');

My function in listController.php (currently just a test function):

public function removeItem(){
  echo "hello";
}

When you click on the button in the list.blade.php you get sent to a page that says

error 419 Sorry, your session has expired. Please refresh and try again.

php artisan route:list returns:

enter image description here

Arnel Forbes replied 2 months ago

maybe it is about CSRF, try using @csrf in your form

SoftWeb Globe replied 2 months ago

Put the @csrf tag immediately after the form tag before any other form elements. That will solve your problem.


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us