Hello I'm trying too make a function that will count how long a user is on my page and when he leavs, saves it in my database. I'll admit, that I'm totaly new too ajax, but I want too learn. Can someone please take a look at my code and maybe point me at the right direction dunkapa.php, this is the site that I want too count time on.
<head>
<script>
var startTime = new Date();
window.onbeforeunload = function() {
var endTime = new Date(); //Get the current time.
var timeSpent = (endTime - startTime); //Find out how long it's been.
var ip = (window.location != window.parent.location) ? document.referrer: document.location;
$.post('ajaxtime.php', {ip: ip, timeSpent: timeSpent});
};
</script>
</head>
ajaxtime.php
<?php
$mysql_hostname = "*****";
$mysql_user = "*****";
$mysql_password = "*****";
$mysql_database = "*****";
$prefix = "";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");
$tid = $_POST['timeSpent'];
$sql_in = "insert into `TID` (TID) values ('$tid')";
mysql_query( $sql_in);
?>
I have asked around a bit but no one can help me, with the ajax, but it seams like the javacode is correct.