2014-01-15 123 views
-5

我有這個變量 - 時間,它計算在一個頁面上花費的時間。如何發送JS變量值到PHP?

我想將其發送到PHP文件。我怎樣才能做到這一點?

That's我做了什麼:

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

謝謝!

+0

您可以使用Ajax將信息從JS發送到PHP。真正的問題是:你想要做什麼? – Pascamel

+0

@Pascamel,我想知道如何計算用戶在每個頁面上停留的時間。例如,我的網站有12頁,我需要花時間閱讀每個頁面的內容。另外,我需要將這些值發送到數據庫。 – user3200239

回答

0

你可以用GET變量,重定向,像這樣

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

,然後在你的PHP,你可以如果你這樣運行後加載的PHP腳本與

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

使用它,只需在末尾使用GET變量加載腳本即可。說腳本是myscript.php。相反,加載類似myscript.php?timespent=[append time here]。在腳本中,你可以得到像這樣的變量

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

並繼續。