【问题标题】:Javascript Stopwatch to MySQL DatabaseJavascript 秒表到 MySQL 数据库
【发布时间】:2011-04-19 00:09:03
【问题描述】:

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

提前谢谢!

【问题讨论】:

  • 为什么不单独存储开始和停止时间呢?然后您可以稍后计算经过的时间,并且不要让客户乱七八糟。
  • 您不希望以这种方式测量持续时间。 setTimeout 不应该是 100% 准确的,这取决于系统负载 + 浏览器最终可能会在应有的位置降低几个 %,如果你整天运行它,这最终可能会很重要。通过存储开始和停止时间并获取差值来计算持续时间。

标签: php javascript mysql countdown stopwatch


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

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

【讨论】:

  • 技术人员开始修车后将使用此页面,因此我不担心客户会弄乱事​​情。因此,我知道 javascript 也将始终启用。感谢您迄今为止的帮助!
  • 我更改了我的计时器代码,还包括了您推荐的@kaykayman,但我仍然无法将其添加到我们的数据库中。
  • 我给出的 php 代码假设你已经连接到一个 mysql 数据库。您将需要使用 mysql_connect 函数以及数据库的用户名/密码/等,以便与它进行交互。如果您已经这样做并且收到错误消息,您能说出这个错误是什么吗?如果它运行没有问题,但没有更新数据库,那么在mysql_query函数之后添加“or die(mysql_error()); 将显示sql生成的任何错误 - 这不会导致php失败。例如mysql_query( 'INSERT INTO... ... ') or die(mysql_error());
猜你喜欢
  • 2014-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-13
  • 2012-01-14
  • 1970-01-01
  • 2018-01-20
相关资源
最近更新 更多