Javascript 秒表到 MySQL 数据库
所以我需要做的是在我创建的 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>
提前致谢!
So what I need to do is have a start time and an end time on the html page that I am creating. It is so that the technician can start the clock once he begins work, and when he ends that day he hits stop and the total time elapsed is calculated. Now I have found a bunch of code that will do this, but I need to go one step further. I need to get the time elapsed and POST it through my PHP file so that it gets saved in a table that was created in our MySQL database.
<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>
Thanks ahead of time!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只要您依赖启用 JavaScript,您就可以创建一个隐藏的表单元素,并在用户单击“停止”时为其分配计算的持续时间。这本身应该是一个提交按钮。
所以你的 Javascript 将是这样的:
你的 HTML 还将有:
你还需要一个处理持续时间值的 PHP 文件:
上面的代码假设你已经有一种识别用户的方法。
作为记录,我同意之前关于在服务器上记录开始时间和结束时间,然后计算那里的持续时间的评论。正如前面所说,在客户端执行此操作的问题在于,用户能够通过操作 JavaScript 代码,甚至只是他们的系统时间和日期来更改数据。另外,如果未启用 JavaScript,则任何计时器功能都将无法工作。
如果您确信 JavaScript 将始终处于启用状态并且用户不会操纵它,那么上面的解决方案(或非常类似的解决方案)应该可以工作。
PS:您可能需要仔细检查 JavaScript 和 PHP 的语法,因为我只是凭空输入它,还没有在代码编辑器中看到它。
As long as you're relying on JavaScript being enabled, you could create a hidden form element and assign the calculated duration to it when the user clicks "Stop". This itself should be a submit button.
So your Javascript will be something like this:
And your HTML will also have:
You will also need a PHP file which processes the duration value:
The above code assumes you already have a means of identifying the user.
For the record I agree with the previous comment about recording a start time and end time on the server and then calculating the duration there. The problem with doing it on the client side - as has been said - is that the user is capable of altering the data either by manipulating th JavaScript code, or even just their systems time and date. Also if JavaScript isn't enabled then none of the timer functionality would work at all.
If you're positive that JavaScript will always be enabled and that the User will not manipulate it then the solution as above (or something very like it) should work.
P.S. You may need to double check the syntax for JavaScript and PHP as I just typed it off the top of my head and haven't seen it in a code editor.