PHP update database on close window

余生颓废 提交于 2019-11-28 02:23:55

问题


I am trying to update the database when the user close or refresh the browser. This is my code:

$(window).bind('unload', function(){
    $.ajax({
        type: 'get',
        async: false,
        url: 'liberaLugar.php?idCurso=2'
    });
});

Any help will be appreciated. Thanks in advance!


回答1:


This should work for you in most cases. of course, if your server is unreachable, you could freeze the browser, so we should also add a timeout. it may also be helpul to give your user some visual indicator of what the hell is going on, so we should set the cursor to waiting.

var unloaded = false;
$(window).on('beforeunload', unload);
$(window).on('unload', unload);  
function unload(){      
    if(!unloaded){
        $('body').css('cursor','wait');
        $.ajax({
            type: 'get',
            async: false,
            url: 'liberaLugar.php?idCurso=2',
            success:function(){ 
                unloaded = true; 
                $('body').css('cursor','default');
            },
            timeout: 5000
        });
    }
}

EDIT: Synchronous AJAX is now deprecated in Webkit Browsers.



来源:https://stackoverflow.com/questions/27217289/php-update-database-on-close-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!