Javascript 如何将JS变量值发送到PHP?

Javascript 如何将JS变量值发送到PHP?,javascript,php,Javascript,Php,我有一个变量-time-,它计算在页面上花费的时间 我想把它发送到一个PHP文件。我该怎么做 这就是我所做的: <html> <head> <script language=javascript type="text/javascript"> var min1,min2; function tempoEntrada(){ now = new Date min1 = now.getMinu

我有一个变量-time-,它计算在页面上花费的时间

我想把它发送到一个PHP文件。我该怎么做

这就是我所做的:

<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>


谢谢

您可以使用GET变量重定向,如下所示

location.href="path/to/file.php?timespent"=time;
$timespent = 0;
if (isset($_GET['timespent'])){
    $timespent = $_GET['timespent'];
}
然后在PHP中,您可以将其与

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

如果在运行之后加载PHP脚本,只需在最后加载带有GET变量的脚本即可。假设脚本是myscript.php。相反,加载类似myscript.php?timespunt=[append time here]的内容。在脚本中,可以得到如下变量

location.href="path/to/file.php?timespent"=time;
$timespent = 0;
if (isset($_GET['timespent'])){
    $timespent = $_GET['timespent'];
}

然后继续。

您可以使用Ajax将信息从JS发送到PHP。真正的问题是:你到底想做什么?@Pascamel,我想知道如何计算用户在每个页面停留的时间。例如,我的网站有12个页面,我需要获得用户阅读每个页面内容所需的时间。此外,我需要将这些值发送到数据库。