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

distal-shores's avatar

Triggering Session Flash Message with AJAX Request

I know this question has been asked but none of the proposed solutions have worked for me.

In my client-side code:

 $.ajax({
        type: 'POST',
        url: '/user/' + employeeId + '/' + clientId + '/removeFromOrg',
        success: function(data) {
            location.reload();
        },
        error: function(data) {
            console.log('Error:', data);
        }
    });

In the controller:

    public function removeOrganization(User $user, Organization $org)
    {
        if($user) {
            if($user->organization_id === $org->id) {
                $user->organization_id = null;
                $user->save();
            }
        }

        Request::session()->flash('message.level', 'success');
        Request::session()->flash('message.content', 'User ' . $user->first_name . ' ' . $user->last_name . ' removed from ' . $org->name . '.');

        return response()->json(['success' => 'yay']);

    }

Where am I going wrong here? I can only get the session to display the message if I use session()->put()->save() but then I struggle to clear it properly.

0 likes
3 replies
bobbybouwmann's avatar
Level 88

So you can't flash anything on an ajax request. An ajax request happens on the background and doesn't update the session of the webpage, so it also won't show the message on your page.

If you do an ajax request and want to show a flash message you should handle that on the javascript side

 $.ajax({
    type: 'POST',
    url: '/user/' + employeeId + '/' + clientId + '/removeFromOrg',
    success: function(data) {
        // Show flash message
    },
    error: function(data) {
        console.log('Error:', data);
    }
});
1 like
distal-shores's avatar

Thanks, that's kind of what I figured but there are a lot of SO entries out there that suggest otherwise.

I've reverted the behaviour of this page to not use AJAX as I want to keep my flash messaging consistent.

Please or to participate in this conversation.