1

所以我需要做的是在我正在创建的 html 页面上设置开始时间和结束时间。这样一来,技术人员可以在开始工作后启动时钟,并在当天结束时点击停止并计算经过的总时间。现在我找到了一堆可以做到这一点的代码,但我需要更进一步。我需要获取经过的时间并通过我的 PHP 文件发布它,以便它保存在我们的 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>

提前谢谢!

4

1 回答 1

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 将始终启用并且用户不会对其进行操作,那么上述解决方案(或类似的解决方案)应该可以工作。

PS 您可能需要仔细检查 JavaScript 和 PHP 的语法,因为我只是在脑海中输入了它并且还没有在代码编辑器中看到它。

于 2011-04-19T01:11:45.593 回答