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

lawkunchi's avatar

Laravel Session

Is there a way to get authenticated user information send to admin's email when they try for example to create a post but decided to quit.

0 likes
4 replies
Sergiu17's avatar

By 'quit' you mean closing the window tab? Or press to Cancel button? Or .. ?

Sergiu17's avatar
Sergiu17
Best Answer
Level 60

@LAWKUNCHI - Add two event listeners, one for closing window and one for back button

window.onbeforeunload = function() {
    // send ajax request
}

document.querySelector('.back-button').addEventListener('click', function() {
    // send ajax request
});

Register a route, accept request, take authenticated user and set an mail to admin. For closing window search more in google

1 like
lawkunchi's avatar

@SERGIU17 - Thank you for the tip, my final code below and it works perfectly

My View
<script>
 $(document).ready(function(){
        var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
        $('.back-button').click(function() {
            $.ajax({
                url: 'cancel',
                type: 'GET',
                data: {_token: CSRF_TOKEN, message:$("#email").val()},
                dtatType: 'JSON',
             
               success: function(data) {
                   $('message').append(data.msg);
                }
            });
        });
    });
</script>
Route::get('cancel', [
            'uses' => 'ControllerName@cancelButton',
            'as' => 'cancel',
        ]);

My Controller

public function cancelButton(Request $request) {
        $response = array(
            'status' => 'failed',
            'msg' => $request->message,
        );
        $user = Auth::user();
        Event::fire(new EventName($user));

        \Mail::to('[email protected]')->send(new MailName($user));
        return response()->json($response);
    }
1 like

Please or to participate in this conversation.