2011-04-19 14 views
1

所以我需要做的是在我創建的html頁面上有一個開始時間和結束時間。這是技術人員在他開始工作之後就可以開始工作的時間,當他在那天結束時他會停下來計算總時間。現在我已經找到了一堆能做到這一點的代碼,但我需要更進一步。我需要花費時間並通過我的PHP文件發佈它,以便將其保存在我們的MySQL數據庫中創建的表中。Javascript秒錶到MySQL數據庫

<script type="text/javascript"> 
var duration = 0; 

// Javascript to compute elapsed time between "Start" and "Finish" button clicks 
function timestamp_class(this_current_time, this_start_time, this_end_time, this_time_difference) { 
     this.this_current_time = this_current_time; 
     this.this_start_time = this_start_time; 
     this.this_end_time = this_end_time; 
     this.this_time_difference = this_time_difference; 
     this.GetCurrentTime = GetCurrentTime; 
     this.StartTiming = StartTiming; 
     this.EndTiming = EndTiming; 
    } 

    //Get current time from date timestamp 
    function GetCurrentTime() { 
    var my_current_timestamp; 
     my_current_timestamp = new Date();    //stamp current date & time 
     return my_current_timestamp.getTime(); 
     } 

    //Stamp current time as start time and reset display textbox 
    function StartTiming() { 
     this.this_start_time = GetCurrentTime();  //stamp current time 
     document.TimeDisplayForm.TimeDisplayBox.value = 0;  //init textbox display to zero 
     } 

    //Stamp current time as stop time, compute elapsed time difference and display in textbox 
    function EndTiming() { 
     this.this_end_time = GetCurrentTime();   //stamp current time 
     this.this_time_difference = (this.this_end_time - this.this_start_time)/1000; //compute elapsed time 
     document.TimeDisplayForm.TimeDisplayBox.value = this.this_time_difference;  //set elapsed time in display box 
     } 

var time_object = new timestamp_class(0, 0, 0, 0); //create new time object and initialize it 

//--> 

function assignDuration() 
{ 
    document.stopform.duration.value = duration; 
} 
</script> 

    <form> 
     <input type="button" value="Start" onClick="time_object.StartTiming()"; name="StartButton"> 
    </form> 
    <form> 
     <input type="button" value="Finish" onClick="time_object.EndTiming()"; name="EndButton"> 
    </form> 

<form name="stopform" action="process-form.php" method="POST"> 
    <input type="hidden" name="duration" value="0"/> 
    <input type="submit" name="dostop" onClick="assignDuration()" value="Stop"/> 
</form> 

提前致謝!

+0

爲什麼不單單存儲啓動和停止時間?然後,您可以計算經過的時間,並且不允許客戶弄亂事情。 – 2011-04-19 00:37:38

+0

你不想用這種方法測量持續時間。 setTimeout不應該是100%準確的,這取決於系統負載+瀏覽器最終可能會有幾個百分點的折扣,如果你整天運行它,這可能會很重要。通過存儲開始和停止時間並獲取差異來計算持續時間。 – Nathan 2011-04-19 03:09:52

回答

1

只要您啓用了JavaScript,您可以創建一個隱藏的表單元素,並在用戶單擊「停止」時爲其分配計算的持續時間。這本身應該是一個提交按鈕。

所以你的Javascript會是這樣的:

<script type="text/javascript"> 
var duration = 0; 
...YOUR TIME CALCULATION CODE... 

function assignDuration() 
{ 
    document.stopform.duration.value = duration; 
} 

</script> 

而且你的HTML也將有:

<form name="stopform" action="process-stop.php" method="POST"> 
    <input type="hidden" name="duration" value="0"/> 
    <input type="submit" name="dostop" onClick="assignDuration()" value="Stop"/> 
</form> 

您還需要處理該時間值的PHP文件:

$duration = $_POST['duration']; 
mysql_query('INSERT INTO MyTable (duration, technician) VALUES ('.$duration.', '.$technicianId.')'); 

上面的代碼假定你已經有了識別用戶的方法。

爲了記錄,我同意先前有關在服務器上記錄開始時間和結束時間的評論,然後計算那裏的持續時間。如前所述,在客戶端執行它的問題是,用戶可以通過操縱JavaScript代碼甚至只是系統時間和日期來更改數據。另外,如果JavaScript沒有啓用,那麼定時器的功能根本不起作用。

如果您肯定JavaScript將始終啓用並且用戶不會操作它,那麼上述解決方案(或其他非常類似的解決方案)應該可以工作。

P.S.您可能需要仔細檢查JavaScript和PHP的語法,因爲我只是將它從頭頂輸入,並沒有在代碼編輯器中看到它。

+0

技術人員在開始上車時會使用此頁面,所以我不擔心客戶弄亂了事情。正因爲如此,我知道JavaScript將始終啓用。感謝你目前的幫助! – MTSP 2011-04-21 00:37:06

+0

我更改了自己的計時器代碼,並且還包含了您推薦的@kaykayman,但我仍然無法將其添加到我們的數據庫中。 – MTSP 2011-04-22 19:52:47

+0

我給出的php代碼假設你已經連接到一個mysql數據庫。您需要使用mysql_connect函數,以及數據庫的用戶名/密碼/ etc以便與其進行交互。如果你已經這樣做了,並且正在收到一條錯誤消息,你能說出這個錯誤是什麼嗎?如果它運行沒有問題,但沒有更新數據庫,那麼在mysql_query函數之後添加「或die(mysql_error());將會顯示sql生成的任何錯誤 - 這不會導致php失敗,例如mysql_query 'INSERT INTO ...')或死(mysql_error()); – kaykayman 2011-04-23 14:37:08