-4

I have this variable -time-, which calculates the time spent at a page.

I would like to send this to a PHP file. How can i do this?

That´s what i have done:

<html>
<head>
    <script language=javascript type="text/javascript">
        var min1,min2;
    function tempoEntrada(){ 
        now = new Date
        min1     = now.getMinutes();        // 0-59
        seg1     = now.getSeconds();
    }
    function tempoSaida(){
        now = new Date
        var min2    = now.getMinutes();        // 0-59
        var seg2     = now.getSeconds();

        min=min2-min1;
        seg=seg2-seg1;
        time=min+':'+seg;


    </script>
</head>
<body onload="tempoEntrada()">
    <form>
    <input type="button" onclick="tempoSaida()" value="here">
</form>

Thanks!

  • You can use Ajax to send information from JS to PHP. the real question is : what are you trying to do exactly? – Pascamel Jan 15 '14 at 22:47
  • @Pascamel, I would like to know how to count the time that a user stays at each page. For example, my website has 12 pages and I need to get the time that a user takes to read the content of each page. Also, I need these values sent to a database. – user3200239 Jan 16 '14 at 02:54

2 Answers2

0

You could redirect with GET variables, like this

location.href="path/to/file.php?timespent"=time;

and then in your PHP you could use it with

if(isset($_GET['timespent'])){
     $timespent=$_GET['timespent'];
}
scrblnrd3
  • 7,228
  • 9
  • 33
  • 64
0

If you're loading the PHP script after this is run, just load the script with GET variables at the end. Say the script is myscript.php. Instead of that, load something like myscript.php?timespent=[append time here]. In the script, you can get variables like this

$timespent = 0;
if (isset($_GET['timespent'])){
    $timespent = $_GET['timespent'];
}

And continue.

defect
  • 488
  • 5
  • 16