Skip to content
Advertisement

Laravel 7 Post contact form giving me error 419

so I’ve been trying to get a contact form to display as /contact when using the form, and after submitting the form. I’m getting Error 419 no matter what I seem to try.

I created a resource controller called ContactController to get this to work, but if I use Route::resource I’d be stuck using /contact/create as the actual form.

Here are my contact Routes for now:

//Route::resource('contact', 'ContactController');

Route::post('/contact', 'ContactController@store');

Route::get('/contact', 'ContactController@create');

Here is my contact form:

<form class="form" method="POST">
            @error('name')
            <div class="alert alert-danger">{{ $message }}</div>
            @enderror
            <div class="form-group">
                <label for="name">Name</label>
                <input type="text" name="name" id="name" class="form-control @error('name') is-invalid @enderror" placeholder="Enter Your Name....">
            </div>
            @error('email')
            <div class="alert alert-danger">{{ $message }}</div>
            @enderror
            <div class="form-group">
                <label for="email">Email</label>
                <input type="text" id="email" name="email" class="form-control @error('email') is-invalid @enderror" placeholder="johndoe@example.com">
            </div>
            @error('subject')
            <div class="alert alert-danger">{{ $message }}</div>
            @enderror
            <div class="form-group">
                <label for="subject">Subject</label>
                <input type="text" id="subject" name="subject" class="form-control @error('subject') is-invalid @enderror" placeholder="What is it you need?">
            </div>
            @error('content')
            <div class="alert alert-danger">{{ $message }}</div>
            @enderror
            <div class="form-group">
                <label for="content">Content</label>
                <textarea class="form-control  @error('content') is-invalid @enderror" name="content" id="content" rows="3" placeholder="Tell me more..."></textarea>
            </div>
            <button type="submit" class="btn btn-primary">Submit</button>
        </form>

I’m sure there’s something I’m doing wrong, so what could it be?

Advertisement

Answer

To prevent cross-site request forgery attacks, Laravel requires CSRF “tokens” to be present in your forms. Change

<form class="form" method="POST">

to

<form class="form" method="POST">
    @csrf

to include a hidden CSRF token field in the form.

https://laravel.com/docs/7.x/csrf

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement