-2

I'm currently working on my first web project with Laravel. The app will has a very simple task, the user puts in a date and my website will check if that date is in a set timeframe. So its first getting user input, secound check if the input date is in between 2 other dates and last print a massage out on the view when depending on the outcome of step 2. Sadly every time i press the submit-button which should give me some output i always get an 419 error, I hope someone can tell me what I am doing wrong.

Here is my view named welcome.blade.php

<!DOCTYPE html>
<html>  
<body>
      
    <b> Klausuretermin auswählen
    </b>
      
    <br>
    <form method="POST" action="/dateCalc">
      <input type="date" id="date" value="date">
    
    <br>
    <input type="text" name="klausurname" value="klausurname">
    <br>
    <button type="submit" name="submit">Submit</button> 
    </form>
    <br>
    @if (isset($result))
        <p>{{ $result }}</p>
    @endif
</body>
  
</html>        

Here is my model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class DateCalc extends Model
{
    public function checkDateBetween( $toCheck){
        //still needs logic want to print out input to test the rest
        return $toCheck;
    }
}

here my controller:

<?php

namespace App\Http\Controllers;

use App\DataCalc;
use Illuminate\View\View;
use Illuminate\Http\Request;

class DateController extends Controller
{
    public function index(): View{
        return view('welcome');
    }    
    public function checkDate(DateCalc $dateCalc): View{
        $date = "OK";
        
        return view('welcome', ['result' => $$date]);
    }
}

and at last my routes:

Route::get('/dateCalc','DateController@index');
Route::post('/dateCalc', 'DateController@checkDate');
Propfeks
  • 7
  • 2
  • 2
    You need to include a CSRF token field in your form: https://laravel.com/docs/9.x/csrf#main-content – Martin Bean May 09 '22 at 00:36
  • 1
    Does this answer your question? [Laravel 7 Post contact form giving me error 419](https://stackoverflow.com/questions/62791394/laravel-7-post-contact-form-giving-me-error-419) – Kirk Beard May 09 '22 at 00:46
  • 1
    Please try searching before posting - "*laravel 419*" turns up so many answers. – Don't Panic May 09 '22 at 07:25
  • 1
    Does this answer your question? [Ajax LARAVEL 419 POST error](https://stackoverflow.com/questions/46472812/ajax-laravel-419-post-error) – Don't Panic May 09 '22 at 07:25

2 Answers2

1

you have to add csrf protection in your form

<!DOCTYPE html>
<html>  
<body>
      
    <b> Klausuretermin auswählen
    </b>
      
    <br>
    <form method="POST" action="/dateCalc">
      @csrf
      <input type="date" id="date" value="date">
    
    <br>
    <input type="text" name="klausurname" value="klausurname">
    <br>
    <button type="submit" name="submit">Submit</button> 
    </form>
    <br>
    @if (isset($result))
        <p>{{ $result }}</p>
    @endif
</body>

you can read more in documentation https://laravel.com/docs/9.x/csrf#main-content

Alfa Riza
  • 56
  • 2
0

Please add @csrf token in your form.

Example :

<form method="POST">
  @csrf  // CSRF 
</form>
Pream Dev
  • 19
  • 2