skip to Main Content

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


    1. Double-check that the @csrf directive is present in the form and is rendering correctly in the HTML output.

      <form method="POST" action="{{ url('/registercomplain') }}">
          @csrf
          <!-- rest of your form fields -->
      </form>
      

      Then, view the page source in your browser. You should see a hidden input field like this:

      <input type="hidden" name="_token" value="randomTokenStringHere">
      
    2. Verify that the VerifyCsrfToken middleware is properly configured in the app/Http/Kernel.php file.

      • Checking VerifyCsrfToken middleware:
        Open app/Http/Kernel.php and look for the web middleware group. It should
        include AppHttpMiddlewareVerifyCsrfToken::class,. If it’s missing, add it.
    3. Check the session configuration in config/session.php, especially the session driver and lifetime settings.

      {
      'driver' => 'file',
      'lifetime' => 120,
      'expire_on_close' => false,
      'secure' => false,
      }
      
    4. Try clearing the application cache and regenerating the application key.

      php artisan config:clear
      php artisan cache:clear
      php artisan view:clear
      php artisan route:clear
      
    Login or Signup to reply.
  1. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search