How to check window close event in php, in an "if" condition?
How can I check if the user is closing the window after filling in one or more forms (parts) of a multipart form, but not all the parts.
I have to send the data collected from all/some parts of multipart form to the external server.
One condition is that, I can send the data to the external server on开发者_Go百科ly once for one complete form.
edit: after 2 answersi have edited my question a bit
Look into using window.onbeforunload
PHP is a server-side language. You should use Javascript's onbeforeunload() for browser-side operations.
This is always a complicated subject, as HTTP is a stateless protocol.
Javascript can be one, not so reliable, solution. Using events, or ajax to keep sending everything are some possible solutions.
From server-side point of view, you can just deal with data and signals sent from User-Agent. You have a few ways to check connection status with PHP.
In short you can check for 3 signals: - NORMAL - ABORTED - TIMEOUT
Normal means nothing unexpected happened. Aborted signal means user clicked STOP button while loading. Timeout is triggered by server if process takes too long (this signal is available in the same way a timeout is sent by the server to the user).
Note that if user closes a window after page is loaded (page loads, fills half form, and closes window), or if a power down, a kick on the net cable or anything else happens AFTER the page is loaded, the status will still be NORMAL.
Yeah, it sucks. You can't control what user is doing from the server-side. You need a way to control that from the client, here is where javascript come handy.
I expect in a near future to be able of using web sockets and web workers to handle this kind of problem, along with register_tick_function. While we do not reach a good point of support from UAs in this subject, only a few scripting can be used.
精彩评论