ngreenwood6 Posted September 15, 2008 Share Posted September 15, 2008 I am trying to figure out how to set-up a php event when the window is closed. For instance I want something to be inserted into the database when the window is closed. Can someone show me how to use the window unload event with php? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/124314-window-help/ Share on other sites More sharing options...
kenrbnsn Posted September 15, 2008 Share Posted September 15, 2008 That can only be done via Javascript. PHP has no knowledge of windows. If you need to get the information to PHP use AJAX. Ken Quote Link to comment https://forums.phpfreaks.com/topic/124314-window-help/#findComment-641970 Share on other sites More sharing options...
ngreenwood6 Posted September 15, 2008 Author Share Posted September 15, 2008 Can you give me an example or a good place to start? I have no java knowledge and am looking for a solution. Quote Link to comment https://forums.phpfreaks.com/topic/124314-window-help/#findComment-641983 Share on other sites More sharing options...
KevinM1 Posted September 15, 2008 Share Posted September 15, 2008 Can you give me an example or a good place to start? I have no java knowledge and am looking for a solution. First, a terminology lesson. Java != JavaScript. At all. They're not related. So, no, JavaScript is not a subset of Java, or anything like that. Java is an object-orientated language which runs on a virtual machine. It was created by Sun Microsystems. JavaScript is a browser-based scripting language originally developed by Netscape. JavaScript 'borrowed' the 'Java' portion of the name as a marketing ploy. No more, no less. I'm not trying to jump down your throat or anything, but it's something that a lot of beginning developers get wrong, and is a pet peeve of mine. To address your problem, you'll need to use AJAX. AJAX is an acronym for Asynchronous JavaScript and XML. The name is a bit misleading as you don't need XML to get it to work. AJAX is necessary for this because of its asynchronous nature. It allows you to send requests to the server, and obtain the results of those requests, without having to reload the page. Since you probably don't want to learn the ins-and-outs of AJAX, you can probably get away with leaning heavily on the jQuery (documentation: http://docs.jquery.com/Main_Page - download: http://docs.jquery.com/Downloading_jQuery) library to help you with the syntax. For this, you'll need a few things: 1. A callback function tied to the onclose event. 2. A flag to send to your PHP script to tell it to save the necessary data. 3. The PHP code that checks for the flag and saves the data. You should do something like: <script type="javascript" src="jquery-1.2.6.min.js"></script> <script type="javascript"> window.onclose = function() { $.post("yourScript.php", {toSave: "yes"}); //replace yourScript.php with the name of your script } </script> As you can see, no XML was used during the transfer of data. Instead, a simple name-value pair was sent to the server as POST data. And then, in your PHP: if(isset($_POST['toSave']) && $_POST['toSave'] === "yes") { //save your data to the db } Just an aside: You may have to use onunload instead of onclose. I'm not sure if onclose works with all browsers. Quote Link to comment https://forums.phpfreaks.com/topic/124314-window-help/#findComment-641993 Share on other sites More sharing options...
ngreenwood6 Posted September 15, 2008 Author Share Posted September 15, 2008 Sorry about the java thing. I know that java and javascript are 2 totally different things. I just wasnt paying attention when I posted the last post. Thanks for the help though. I will look into it. Quote Link to comment https://forums.phpfreaks.com/topic/124314-window-help/#findComment-642039 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.