I've made a short game in which people give answers to sequentially-presented questions, and in which their scores are stored in variables that are updated with every right answer (ie.
var Score = 0; function ScoreUpdate {if (event.keyCode == correctAnswer) {Score = Score +1;}}
). At the end, a JavaScript function is called that puts these variables into a form and subsequently "POST"s it to a page which then adds everything to a MySQL database.
The problem is that some people will stop half-way and simply close the window/tab, causing none of the scores to be submitted (as the JavaScript function to put all variables into the form and submit it isn't activated). I'm thus looking for a way that will automatically activate the function to save the scores, submit the form and upload to the database when the user closes the window. This involves both PHP and JS, hence I'm wondering whether all of this is even possible in just one event? (ie. window.onbeforeunload?)
I've tried this solution: https://stackoverflow.com/a/1632004/1092247 but it does not include automatic form submission (it doesn't even work in general for me, as it still prompts even when form is submitted).
As you can see I'm relatively new here, with far from advanced JS and PHP knowledge. Any help would be very much appreciated! :)