bPHP Posted April 20, 2010 Share Posted April 20, 2010 Hi, I'm using the following code to check if the user wants to navigate away from my page: window.onbeforeunload = unloadMess; function unloadMess () { if (!desconectado) { if (!enviando) { mess = "Los cambios realizados no se guardarán" return mess; } } } form.onsubmit = onSubmit; function onSubmit () { enviando = true; } However, I do not know where to add something I want to do if he navigates away. I need to call a function that calls a php if the user leaves. I'm trying the following: window.onunload = limpiar; limpiar() { phpFunc('bye'); } But it is not working... Any ideas? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/199160-onbeforeunload/ Share on other sites More sharing options...
F1Fan Posted April 20, 2010 Share Posted April 20, 2010 Using onunload, you can check if a user is attempting to leave the page, but once that page is closed, your code is no longer loaded in there browser, and therefore you cannot execute any code. Quote Link to comment https://forums.phpfreaks.com/topic/199160-onbeforeunload/#findComment-1045305 Share on other sites More sharing options...
andrewgauger Posted April 20, 2010 Share Posted April 20, 2010 You are trying to mix technologies. onbeforeunload is client side and operates using Javscript. PHP is executed on the server and there is no PHP that you can execute from this context. Also, window.onbeforeunload = unloadMess(); You need to call the function with () in javascript (and PHP for that matter) Also, with your code someone can click submit and then navigate away. The only way to properly test is in the PHP handler. (the code the form calls) Quote Link to comment https://forums.phpfreaks.com/topic/199160-onbeforeunload/#findComment-1045306 Share on other sites More sharing options...
bPHP Posted April 20, 2010 Author Share Posted April 20, 2010 I know where is php executed. But at the same time I know how to call a php function from javascript, something I'm doing on my code. However, my problem is that I would like to clean the user data when he leaves (stuff in the database), and I don't know how to do it. Is there any way that if he presses OK in the navigate away from page message box I can call that php function? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/199160-onbeforeunload/#findComment-1045371 Share on other sites More sharing options...
aeroswat Posted April 20, 2010 Share Posted April 20, 2010 I know where is php executed. But at the same time I know how to call a php function from javascript, something I'm doing on my code. However, my problem is that I would like to clean the user data when he leaves (stuff in the database), and I don't know how to do it. Is there any way that if he presses OK in the navigate away from page message box I can call that php function? Thanks! You need to use ajax to do this. Look up jquery and ajax functions Quote Link to comment https://forums.phpfreaks.com/topic/199160-onbeforeunload/#findComment-1045377 Share on other sites More sharing options...
seventheyejosh Posted April 20, 2010 Share Posted April 20, 2010 If you use jquery ajax in the unload function, be sure to set asynchronous = false using the full low-level ajax(); Edit: I just found out yesterday it is sporadic without the async setting, took a while to debug. Here is how mine was implemented for a client: $(window).unload(function(){ $.ajax({ async:false, type:"GET", url:"/index.php", data:'direct=modules/chat/chat.php&action=closechat&which='+$("#whichChat").val(), cache:false }); }); Quote Link to comment https://forums.phpfreaks.com/topic/199160-onbeforeunload/#findComment-1045401 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.