This is a general question with no attempt so far since I don’t know where to start. All I’ve seen are javascript alerts upon leaving a page.
Scenario:
-
When User1 visit a page I want to generate a random string and make an entry in the database for that string.
-
This random number can be entered by one other user (User2).
-
I will check that the random number has an entry in the database before allowing User2 to “proceed”
When User1 leaves the page, I want to remove the entry from the database, so that if User2 enters the random string, it won’t be present in the database any longer/ won’t be valid anymore.
How do I trigger an event to delete the row upon leaving the page?
Not sure how else to describe this — if I’m making this more difficult than it needs to be or I’m just approaching the problem wrong, please point me in the right direction as to how to pull this off.
Perhaps someone can prove me wrong, but as far as I know there isn’t a reliable way of triggering a server-side event when users leave a page.
Yes, there is the
onunloadevent that you can use to trigger a quick ajax call, but HTTP is by definition a stateless protocol and attempting to create a solution which emulate a stateful connection will have flaws. Consider:From your scenario, I suspect that you might be approaching your problem from a wrong angle. Do give more details on what you’re trying to achieve and someone might be able to help.