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

Caracorn's avatar

Two forms on one website, each with two times {{ csrf_field() }}

Hello,

I have 2 forms on one website, each with two times {{ csrf_field() }}.

If I send a request from the one form and after processing by the controller come back to the blade with new data, everything is ok. If I send a new request from the same form, everything is ok.

However, when I send a request from the other form (after coming back from the one), I get an error 419 | page expired.

How can i solve it?

Many Thanks

0 likes
10 replies
bobbybouwmann's avatar

If you just have an HTML/blade only form (so no javascript), this should work for multiple forms automatically. The CSRF-token would work just fine for both forms. You can use 100 forms using the same token if you like.

Can you share how you created your form in your view?

1 like
Caracorn's avatar
@section('content')
    <div class="container">
        <div class="row">
            <div class="col-md-10 col-md-offset-1">
                <div class="panel panel-default">
                    <div class="panel-heading">TOPIC</div>
                    <div class="panel-body">
                        <form>
                            {{ csrf_field() }}
                            <div class="row">
                                <div class="col-md-4"></div>
                                <div class="col-md-7">
                                    <label>Some values:</label>
                                    <div class="form-group">
                                        <input type="text" class="form-control" name="value" value="{{ request('value') }}">
                                    </div>
                                </div>
                                <div class="col-md-1">
                                    <div class="form-group">
                                        <label>&nbsp;</label>
                                        <button type="submit" name="search" class="form-control btn btn-primary">
                                            <i class="fa fa-search"></i></button>
                                    </div>
                                </div>
                            </div>
                        </form>
                        @if(isset($someValue))
                            <form class="form-horizontal" role="form" method="POST" action="{{ URL::to('/new/method') }}">
                                {{ csrf_field() }}
                                <div class="row">
                                    <div class="col-md-4"></div>
                                    <div class="col-md-7">
                                        <label>&nbsp;</label>
                                        <div class="form-group">
                                            <input type="text" class="form-control" name="value2" value="{{ request('value2') }}">
                                        </div>
                                    </div>
                                    <div class="col-md-1">
                                        <div class="col-md-8 col-md-offset-4">
                                            <button type="submit" class="btn btn-primary pull-right">
                                                <i class="fas fa-archive"></i> Send
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </form>
                        @elseif(isset($nothing))
                            Nothing found!
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection
Caracorn's avatar

Sorry, I forgot how to post a code snippet. The forum completely changed the code I posted above, I don't remember what it was like to put a blade-code in without it being changed.

I only use Javascript to change the attributes of the HTML code, but no AJAX or something like that.

Snapey's avatar

Should be fine with two forms. Either can be submitted. After that form is processed, you should re-render the view by returning a redirect back to the original page.

Caracorn's avatar

Thank you. It works. But coming back with the following I cannot access the variable 'success':

return redirect()->back()->with( 'success', 'It works.');

// Blade:
@section('system_message')
    @if(session()->has('error'))
        <div class="bgerror colorgreyd pad15px">
            {{$error}}
        </div>
    @elseif(session()->has('success'))
        <div class="bgsuccess colorgreyd pad15px">
            {{$success}}
        </div>
    @endif
@endsection

Snapey's avatar

I think session always has $error but it is null if there is no error

Change your elseif to a seperate if


@section('system_message')
    @if(session()->has('error'))
        <div class="bgerror colorgreyd pad15px">
            {{$error}}
        </div>
    @endif

    @if(session()->has('success'))
        <div class="bgsuccess colorgreyd pad15px">
            {{$success}}
        </div>
    @endif
@endsection
Caracorn's avatar

Thank you, but the variable seems to be unknown:

ErrorException (E_ERROR)
Undefined variable: success ...
Previous exceptions
Undefined variable: success (0)

Return back means going back to the previous step. Does that mean that the success variable doesn't yet exist?

Caracorn's avatar

I found a workaround through the Session helper

// Controller
session( [ 'success' => 'It works.' ] );
return redirect()->back();

// Blade
@section('system_message')
    @if(session()->has('error'))
        <div class="bgerror colorgreyd pad15px">
            {{$error}}
        </div>
    @endif
    @if(session()->has('success'))
        <div class="bgsuccess colorgreyd pad15px">
            {{session()->get('success')}} 
            {{session()->forget('success')}}
        </div>
    @endif
@endsection
Snapey's avatar

This line echos whatever comes back from the forget function

{{session()->forget('success')}}

If you want success to only appear once, use instead the flash() function.

https://laravel.com/docs/8.x/session#flash-data

Also look at named error bags so that the forms get their own error variables.

Please or to participate in this conversation.