gpt4 book ai didi

php - Javascript 秒表到 MySQL 数据库

转载 作者:行者123 更新时间:2023-11-29 00:56:53 24 4
gpt4 key购买 nike

所以我需要做的是在我创建的 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>

提前致谢!

最佳答案

只要您依赖启用的 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 将始终启用并且用户不会操纵它,那么上述解决方案(或非常类似的解决方案)应该有效。

附言您可能需要仔细检查 JavaScript 和 PHP 的语法,因为我只是凭空输入,没有在代码编辑器中看到它。

关于php - Javascript 秒表到 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5710208/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com