1

したがって、私が行う必要があるのは、作成しているhtmlページに開始時刻と終了時刻を設定することです。これは、技術者が仕事を始めたら時計を開始できるようにするためであり、技術者がその日を終えると停止し、合計経過時間が計算されます。これを実行するコードをたくさん見つけましたが、さらに一歩進む必要があります。経過時間を取得し、PHPファイルを介してPOSTして、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>

また、duration値を処理するPHPファイルも必要です。

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

上記のコードは、ユーザーを識別する手段がすでにあることを前提としています。

記録については、サーバーで開始時刻と終了時刻を記録し、そこで期間を計算することについての前のコメントに同意します。クライアント側でこれを行う場合の問題は、前述のように、ユーザーがJavaScriptコードを操作するか、システムの日時だけを操作することによってデータを変更できることです。また、JavaScriptが有効になっていない場合、タイマー機能はまったく機能しません。

JavaScriptが常に有効になり、ユーザーがJavaScriptを操作しないことを確信している場合は、上記のソリューション(またはそれに非常に似たもの)が機能するはずです。

PS JavaScriptとPHPの構文を再確認する必要があるかもしれません。これは、頭のてっぺんから入力しただけで、コードエディターで表示されていないためです。

于 2011-04-19T01:11:45.593 に答える