user4221591
user4221591

Reputation: 2150

using route resource form did not post to store function

I have a form which upon successful validation should show the desired result. But on button press the browser displays The page has expired due to inactivity. Please refresh and try again.

view page

<form class="form-horizontal" method="POST" action="{{action('BookController@store')}}">
                <div class="row" style="padding-left: 1%;">
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>Book Name</label><span class="required">*</span>
                                <input type="text" maxlength="100" minlength="3" required="required" runat="server" id="txtBookName" class="form-control" autocomplete="off" autofocus="autofocus" />
                            </div>                            
                            <div class="form-group">
                                    <button type="submit" class="btn btn-primary">Submit</button>        
                            </div>            
                        </div>
                </div>
        </form>

Route code

//web.php
Route::resource('book','BookController');

controller code

class BookController extends Controller
{   

    public function index()
    {
        //
    }


    public function create()
    {
        return view('pages.book');
    }
    public function store(Request $request)
    {
        $validatedInput = $request -> validate([
            'txtBookName' => 'required|string|min:3|max:100'
        ]);

        return $validatedInput;
    }
}

Form url http://127.0.0.1:8000/book/create

on submit button press, the page is redirected to http://127.0.0.1:8000/book and it displays The page has expired due to inactivity. Please refresh and try again.

Upvotes: 1

Views: 1133

Answers (4)

Sergio Ramirez
Sergio Ramirez

Reputation: 51

Implement like this to avoid Expired message.

<form  action="{{ action('ContactController@store') }}" method="POST">                    
    @csrf <!-- this is the magic line, works on laravel 8 -->
    <!--input components-->
    <!--input components-->
    <!--input components-->
</form>

Upvotes: 1

Nikesh Adhikari
Nikesh Adhikari

Reputation: 1

Please do this after the form class, because the resource take the @method('PUT')

<form class="form-horizontal" method="POST" action="{{action('BookController@store')}}">
   {{csrf_field()}}
   @method('PUT')

                    <div class="row" style="padding-left: 1%;">
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label>Book Name</label><span class="required">*</span>
                                    <input type="text" maxlength="100" minlength="3" required="required" runat="server" id="txtBookName" class="form-control" autocomplete="off" autofocus="autofocus" />
                                </div>                            
                                <div class="form-group">
                                    <button type="submit" class="btn btn-primary">Submit</button>        
                            </div>            
                        </div>
                </div>
        </form>

Upvotes: 0

Deepak
Deepak

Reputation: 624

You can either post a CSRF token in your form by calling:

{{ csrf_field() }}

Or exclude your route in app/Http/Middleware/VerifyCsrfToken.php:

protected $except = [
    'your/route'
];

Upvotes: 2

Vinee
Vinee

Reputation: 86

you should use {{ csrf_field() }} in your form.

Upvotes: 0

Related Questions