Doyley Posted February 6, 2008 Share Posted February 6, 2008 Hi all, I am trying to build a script where when a user closes a window it will update a field in the database. Something similar to the following but the following does not work... <script language="Javascript"> function onClose{ <?php $update=mysql_query("update table set column='1' where uid='$uid'"); ?> } </script> <body onUnload="onClose();"> Hello World! </body> Does anybody have any ideas? Thanks! Quote Link to comment Share on other sites More sharing options...
haku Posted February 6, 2008 Share Posted February 6, 2008 Its impossible to do reliably (in that it will not work 100% of the time), but if you are deadset on doing it even if its not reliable, then you need to use AJAX to do it. On unload, the script is executed, sending a request to a php script that updates the database. But anytime someone has javascript turned off it will fail, screwing up your numbers to some degree. Quote Link to comment Share on other sites More sharing options...
aschk Posted February 6, 2008 Share Posted February 6, 2008 You have 2 options. 1) Use ajax to post an event to a php script which will perform the query you're after. 2) onClose redirect to a temporary page which will update the record. On both accounts i'm unsure if the event will actually get triggered by browsers when closing. Quote Link to comment Share on other sites More sharing options...
Doyley Posted February 6, 2008 Author Share Posted February 6, 2008 OK thanks guys. I will try them. I will explain what I am trying to do. I am building some software for a call centre. They will have a list of all clients that they need to call. When they click on the name of a client a new window will open with all of their details but "lock" that record so that no other staff can access the record and cause problems. But when they close that window the record needs to be "unlocked". The Javascript disabled thing shouldn't be a problem, I will just have a word with the IT guys to make sure they are all enabled. Quote Link to comment Share on other sites More sharing options...
Doyley Posted February 6, 2008 Author Share Posted February 6, 2008 OK they only way I could do it was by opening a new window using the unload attribute and then in the new window update my field and automatically close the window. It seems to work well. Thanks for you help guys. Quote Link to comment 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.