Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Albure's avatar

Getting TokenMismatchException Error

I'm running through this tutorial http://laravelblog.com/how-to-create-a-basic-contact-form-validate-input-and-send-mail

Not sure what I am doing that is throwing this error as I followed his directions almost exactly. My controller name is different but I've accounted for that. Here is my controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class contact extends Controller
{
    // This function will show the view
    public function showForm()
    {
        return view('pages.contact');
    }

    public function handleFormPost()
    {
        $input = Input::only('name', 'email', 'msg');

        $validator = Validator::make($input,
            array(
                'name' => 'required',
                'email' => 'required|email',
                'msg' => 'required',
            )
        );

        if ($validator->fails())
        {
            return Redirect::to('contactform')->with('errors', $validator->messages);
        }

    }
}

Here is my routes.php

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

Route::get('/', function () {
    return view('pages.home');
});

Route::get('request', 'controller@request');
//Route::get('contact', 'controller@contact');
Route::get('contact', 'contact@showform');
Route::post('contact', 'contact@handleFormPost');

Here is my contact.blade.php

@extends("layout")

@section("content")
<h1>Contact</h1>
<div class="text-center">
    <img src="http://i.imgur.com/hSuGkPM.png" alt="">
    <p><br>Join our discord server to get an immediate response about your project</p><br>
</div>
<!--
<form class="form-horizontal" role="form">
    <div class="form-group">
        <label class="control-label col-sm-2" for="email">Email:</label>
        <div class="col-sm-10">
            <input type="email" class="form-control" id="email" placeholder="Enter email">
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-sm-2" for="msg">Message:</label>
        <div class="col-sm-10">
            <textarea class="form-control" rows="5" id="message" placeholder="Message"></textarea>
        </div>
    </div>

    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="btn btn-default">Submit</button>
        </div>
    </div>
</form>
-->
<div class="container">
    <h1>A basic contact form</h1>
    <form id="contact" method="post" class="form" role="form">
        <div class="row">
            <div class="col-xs-6 col-md-6 form-group">
                <input class="form-control" id="name" name="name" placeholder="Name" type="text"autofocus="">
            </div>
            <div class="col-xs-6 col-md-6 form-group">
                <input class="form-control" id="email" name="email" placeholder="Email" type="text">
            </div>
        </div>
        <textarea class="form-control" id="message" name="msg" placeholder="Message" rows="5"></textarea>
        <br>
        <div class="row">
            <div class="col-xs-12 col-md-12 form-group">
                <button class="btn btn-primary pull-right" type="submit">Submit</button>
            </div>
        </div>
    </form>
</div>

@stop

What can I do to fix this and get moving again?

EDIT: Adding in {{ csrf_field() }} seems to get around the first error but doing so creates a new error "Fatal error: Class 'App\Http\Controllers\Input' not found." I am not familiar with csrf_field, so I did not know I needed this controller or what to put in it

0 likes
11 replies
bobbybouwmann's avatar

Well this tutorial is pretty old...

Laravel is using csrf protection and that gives that error. You need to add a csrf field to your form

<form id="contact" method="post" class="form" role="form">
    {{ csrf_field() }}

    
</form>

I also notices there is a action property missing on the form element. This means that you will always post to the same page. In this case that will be fine, but it won't work if you want to post to another url!

1 like
Albure's avatar

Would I just copy and paste it in as you have it?

EDIT: When I add that in I get "Fatal error: Class 'App\Http\Controllers\Input' not found" I don't have a input controller nor did I know I needed one. Where is this referenced and what should it look like?

Jaytee's avatar

I'm on my phone so apologies for formatting but you need to add this to all forms

zachleigh's avatar

You meed to send the csrf token in the form. Put this in your form somewhere.

{{ csrf_field() }}
Albure's avatar

Check my above comment. It throws a new error I don't know how to handle

bobbybouwmann's avatar

You need to tell the controller that you use that specific class

use Input;

class contact extends Controller
Albure's avatar

Thanks I'll see if this works

EDIT: After doing that it throws "Fatal error: Class 'Input' not found" didn't we just do that?

Alright got that working. More errors but that's all that has to do with this thread. Thanks!

tomopongrac's avatar

Which version of Laravel do you use?

"Use Input" is for laravel 5

Albure's avatar

I installed laravel about 2 months ago so the most recent version at that time. Should be L5.

And im getting a new error "Class 'App\Http\Controllers\Input' not found"on my live server. Not sure what 's going on as I have declared input already

Where do I put the

$input = $request->input('name', 'email', 'msg');

tomopongrac's avatar

Sorry i didnt write:

Put that code istead:

$input = Input::only('name', 'email', 'msg');

Please or to participate in this conversation.