So im making a game lobby atm and want to run a .php script if a user closes or refreshes his browser.
My problem is, the .unload doesn't even fire, but it should, I basicly used the exact same thing in the wiki. What did I do wrong?
My index:
<script type="text/javascript">
jQuery(document).ready(function ($) {
$(window).on('beforeunload', function() {
return "Do you really want to leave this lobby?";
});
$(window).unload(function() {
alert("wat");
xmlhttp.open("POST","test_ajax.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("session=test");
});
});
</script>
My test_ajax.php:
<?php
$db_name = "test";
$db_username = "root";
$db_password = "";
$db_host = "localhost";
mysql_connect($db_host, $db_username, $db_password) or die ("No connection possible!");
mysql_select_db($db_name) or die ("No database found!");
$v = "Testaccount";
$sql = mysql_query("UPDATE users SET Avatar=1 WHERE Username='$v'");
?>