在Jquery重定向提示符之后运行PHP代码到另一个页面

I am using Laravel as a PHP framework and I have Jquery set up so it prompts the user if they want to confirm redirecting away from the page or not.

The problem is that if the user confirms the redirect away from the page, I need to somehow perform some database operations to set various statues to closed on my tables.

How is this possible?

My Jquery code prompting the user

<script type="text/javascript">
$(window).bind('beforeunload', function(){
return 'Are you sure you want to leave?';
});

Just make an Ajax request to inform the server that the user will be redirectd.

$(window).bind('beforeunload', function(e){
  if ( confirm("Are you sure you want to leave?") ) {
    e.preventDefault();
    
    // make ajax request to inform the server
    $.ajax({
       type: "POST",
       url: reqUrl,
       data: reqBody,
       dataType: "json",
       success: function(data, textStatus) {
          // handle things
       }
    });

    // Redirect.
    window.location.href = someURL;
  } 
});

</div>