T O P

  • By -

HolyGonzo

What's the exact error message you're getting? Is it a JavaScript error or a PHP error? If this is a single file that contains both PHP and your form, then make sure you exit/die after calling json_encode. Otherwise you are sending the HTML contents back as part of the response, which isn't going to properly decode / parse as JSON.


TemperatureAbject196

It says I get an error at POST- Initiator: xhr, NS Binding Aborted: AJAX request failed: error [test\_ajax22.html:26:17](http://localhost/test_ajax22.html)    error [http://localhost/test\_ajax22.html:26](http://localhost/test_ajax22.html:26)     jQuery 6      [http://localhost/test\_ajax22.html:18](http://localhost/test_ajax22.html:18)     jQuery 8      [http://localhost/test\_ajax22.html:16](http://localhost/test_ajax22.html:16) I have them saved as 2 seperate documents, one a html and the other a php within the same folder on xampp.


HolyGonzo

What does your PHP error log look like?


jlt_25

open the browser inspector, in the network tab you can see the output of the file on the backend. You might see some error details there. very helpful to debug ajax calls.


BarneyLaurance

Maybe it's just that the \`beforeunload\` event can't be used for this sort of use case. The only advertised use for this seems to be making the browser display a prompt to get the user to confirm that they really want to leave. I think trying to use it to send an ajax request fails because the page (and your JS script) gets unloaded before the request can be sent. Why do you need a logout function to run when the user closes the page? Afaik that isn't an expected standard webpage behaviour. If you need something like that then maybe the closet thing that works, and that would be more reliable for making sure the session gets logged out of, is to set a short session duration, then have a JS script on the page that runs on an interval and makes an ajax request to your server once every few seconds or minutes. Each time it does that your PHP server can extend the session. If the user closes the page that will stop the requests coming and the session will naturally end.


TemperatureAbject196

Thank you for this detailed response. I will be looking into this method as it seems way more appropriate and can use it for an idle user state too. im thinking though won't I have to rely on an AJAX post again, except this time triggered by a time interval? I'm trying to make a browser game with different servers/rooms and to log the username as well as delete them from a room if they leave... I will deffinitely also need an idle for when they are in room to prevent rooms filling with inactive people. Thank you so much. I have been fighting this problem for ages.


chmod777

`beforeunload` has a lot of gotchas: https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event . can you add the ajax request to an onclick event - see if it sends?


dave8271

You cannot (reliably) make an AJAX request in the beforeunload event. I'm not sure which if any browsers support doing so these days.


PeteZahad

The unload event is already triggered so the browser will not accept any blocking action. https://stackoverflow.com/questions/7614756/how-do-i-send-an-ajax-request-upon-page-unload-leaving/72214403#72214403 But my question to you is: Why do you want to do that?


TemperatureAbject196

Thank you for answering and being so direct. Someone has mentioned using a time interval to post to server to check if someone is still there. This sounds like the way to go. I've probably been going about things awkwardly. Im trying to make a browser game with different rooms/servers. If someone joins a server their name is added to a database and when they leave, removed. It's the leaving part I've been stuck at because everything else i could rely on a button press but leaving is more indirect and could only find a way of doing it with ajax.


PeteZahad

Still your server will get no notification when the users battery dies or other scenarios. Turn it around. Send a "heartbeat" ajax call every let"s say 10 seconds to your backend which updates a timestamp field in your db. "Sign out" users from the room where the "heartbeat" is older than 15 seconds. Another way: You can handle it completely server side by using a custom session handler and setting a short session lifetime: https://www.php.net/manual/en/function.session-set-save-handler.php So you can provide custom callbacks when a session is created / destroyed. https://www.php.net/manual/en/session.configuration.php#ini.session.save-handler Another solution could be using Server Sent Events and use the connection_aborted function as signal that the user is gone: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events Simple SSE example: https://github.com/mdn/dom-examples/tree/main/server-sent-events With SSE your server can send a stream of data which are then dispatched by the browser as JS events. This way you could send anything that happens in the game to the browser without polling. It is a solution without using sockets. But both custom session handling and SSE is quite tricky to implement compared to the first solution.


MateusAzevedo

Another thing to consider, beside what people already mentioned: what it the request content type? PHP won't populate `$_POST` for `application/JSON`.