I am using Laravel 11 for build a application in which a admin panel i have created and created a front-end website. In front-end website a form is exist. when i submit a form than request is not called and HTTP status code 419 unknown status is coming. and when inspect the request in browser. the response is showing "CSRF Token mismatch"
Errors
[enter image description here](https://i.sstatic.net/itmbwv8j.png)
web.php
<?php
use AppHttpControllersUserController;
use AppHttpControllersFrontHomeController;
use AppHttpControllersFrontComplainRegisterController;
use IlluminateSupportFacadesRoute;
// Route::get('/', function () {
// return view('welcome');
// });
// Admin Routes
Route::get('admin', [UserController::class, 'index']);
Route::post('admin/auth', [Usercontroller::class, 'auth'])->name('admin.auth');
//Route::middleware([AdminAuth::class])->group(function (){
Route::get('admin/dashboard', [UserController::class, 'dashboard'])->name('dashboard');
//});
// Website page Routes
Route::get('/', [HomeController::class, 'index']);
Route::get('/onlinecomplain', [ComplainRegisterController::class, 'index'])->name('onlineComplain');
Route::post('/registercomplain', [ComplainRegisterController::class, 'store']);
Form in Blade Template
<form action="{{url('/registercomplain')}}" method="POST" class="contact-form contact-form">
@csrf
<div class="row">
<div class="col-lg-12">
<div class="form-group">
<input class="form-control" id="email" name="email" placeholder="Email*" type="email">
<span class="alert-error"></span>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<input class="form-control" id="captchafill" name="captchafill" placeholder="Enter Captcha" type="text">
<span class="alert-error"></span>
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
{{-- <img src="{{ captcha_src() }}" id="captcha-img" class="captcha"> --}}
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<button type="submit" name="submit" id="submit">
<i class="fa fa-paper-plane"></i> Send OTP
</button>
<!-- <button type="submit" name="btnSubmit" id="btnSubmit" class="d-none">Submit</button> -->
</div>
</div>
<!-- Alert Message -->
<div class="col-lg-12 alert-notification">
<div id="message" class="alert-msg"></div>
</div>
</form>
Controller
<?php
namespace AppHttpControllersFront;
use AppHttpControllersController;
use IlluminateHttpRequest;
class ComplainRegisterController extends Controller
{
public function index()
{
return view('front.onlinecomplain');
}
public function store(Request $request)
{
echo '<pre>'; print_r($request->post); die;
}
}
how to resolve this issue. Please guide me
Thanks
2
Answers
Double-check that the @csrf directive is present in the form and is rendering correctly in the HTML output.
Then, view the page source in your browser. You should see a hidden input field like this:
Verify that the
VerifyCsrfToken
middleware is properly configured in theapp/Http/Kernel.php
file.VerifyCsrfToken
middleware:Open
app/Http/Kernel.php
and look for the web middleware group. It shouldinclude
AppHttpMiddlewareVerifyCsrfToken::class
,. If it’s missing, add it.Check the session configuration in config/session.php, especially the session driver and lifetime settings.
Try clearing the application cache and regenerating the application key.
I think you did not use csrf_token on the head section of your template. Use like this below.
<meta name="csrf-token" content="{{ csrf_token() }}">
on your head section of the template.