hi i just want to ask and ask your help . can you guys help to make timer in my quiz page. seriously i have no idea how to do it. plus im still new in this field around 7 month , so i dont know where to put the code and what i need to write in the code . kindly hope your help.sorry for my broken english
this is my code

<style>

html
{
 font-family:tahoma;
}

</style> <?php
    function gettitle($id)
    {



    $q="SELECT title FROM elearning_title WHERE title_id='$id'";
    list($name)=mysql_fetch_row(mysql_query($q));


    return $name;

    }

?> <?php if(isset($_POST['submit']))
{

        $title_id = $_GET['cid'];       
        $upload_by =  $_SESSION["log"]["userid"];


        $group1 = $_POST['group']['1'];
        $group2 = $_POST['group']['2'];
        $group3 = $_POST['group']['3'];
        $group4 = $_POST['group']['4'];


        $insert=mysql_query("insert into elearning_history set user_id='$upload_by',title_id='$title_id '");






        //$insert=mysql_query("update elearning_history set score='$score' where title_id='$title_id' and user_id='$upload_by' ");
//$insert=mysql_query("insert into elearning_history set user_id='$upload_by',title_id='$title_id ', ans_1='$group1', ans_2='$group2',ans_3='$group3',ans_4='$group4',ans_5='$group5', score='$score'");

        $a=1; 

        while ($a <= count($_POST['group']))
        {   


            $ans="ans_".$a;
            $studentans =  $_POST['group'][$a];

            $query=mysql_query("update elearning_history set $ans='$studentans' where title_id='$title_id' and user_id='$upload_by' ");

        $a++;
        }



        $query="SELECT * FROM elearning_question WHERE title_id='$title_id'";
        $q=mysql_query($query); 

        $score=0;
        $i=1;
        while($row=mysql_fetch_array($q))   
        {

            if( $row['correct_answer'] == $_POST['group'][$i])
            {   
                $score++;

            }

            $i++;

        }

        $final=mysql_query("update elearning_history set score='$score' where title_id='$title_id' and user_id='$upload_by'");

         //$q=mysql_query("SELECT COUNT(*) FROM elearning_question WHERE title_id = '$title_id'");
                    //list($int)=mysql_fetch_row($q); 
                    //echo $int;
?> <script type="text/Javascript"> <!--
                alert("Quiz Successful!");

    --> </script> <body onLoad="self.setTimeout('parent.parent.location.reload().GB_hide()', 60);"> <?php  
}

?> <?php
    $title_id = $_GET['cid'];
?> <h2><center><?php echo gettitle($title_id ); ?></center></h2> <form method="post"> <table border ="0" width="80%" style="background-color:#FFFFFF; border-collapse:collapse; table-layout:fixed" align="center"  cellpadding="8" cellspacing="3"> <tr bgcolor="#99CCFF"> <th width="5%" height="35">Bil</th> <th width="95%">Question</th> </tr> <?php

    $title_id = $_GET['cid'];

    $query="SELECT * FROM elearning_question WHERE title_id='$title_id'";
    $q=mysql_query($query); 

    $count=1;
    $i=1;
    while($row=mysql_fetch_array($q))
    {


        if ( $row['question_type'] == '1')
        {
    ?> <tr> <td align="center"><input name="id"  type="hidden" value="bil"> <?php echo $count; ?></td> <td><?php echo $row['question']; ?></td> </tr> <tr> <td></td> <td width="15%"><input type="radio" name="group[<?php echo $i; ?>]" value="1" required>A.<?php echo $row['answer_one']; ?></td> </tr> <tr> <td></td> <td><input type="radio" name="group[<?php echo $i; ?>]" value="2" required>B.<?php echo $row['answer_two']; ?></td> </tr> <?php 
        $i++;
        }


        else if ($row['question_type'] == '2')
        {
         ?> <tr> <td align="center"><input name="id"  type="hidden" value="bil"> <?php echo $count; ?></td> <td><?php echo $row['question']; ?></td> </tr> <tr> <td></td> <td width="15%"><input type="radio" name="group[<?php echo $i; ?>]" value="1" required>A.<?php echo $row['answer_one']; ?></td> </tr> <tr> <td></td> <td width="15%"><input type="radio" name="group[<?php echo $i; ?>]" value="2" required>B.<?php echo $row['answer_two']; ?></td> </tr> <tr> <td></td> <td width="15%"><input type="radio" name="group[<?php echo $i; ?>]" value="3" required>C.<?php echo $row['answer_three']; ?></td> </tr> <tr> <td></td> <td width="15%"><input type="radio" name="group[<?php echo $i; ?>]" value="4" required>D.<?php echo $row['answer_four']; ?></td> </tr> <?php
        $i++;
        }
    $count++;}
    ?> <tr> <td></td> <td align="center"><input type="submit" name="submit" value="Done" id="submit"></td> </tr> </table> </form> <br /> 

Recommended Answers

All 5 Replies

Member Avatar for diafol

A timer would need to be written in javascript.

You don't mention whether the timer is for question advance or for total time to complete the test.

Be aware that js timers can be stopped by 3rd party scripts, so you'll need to store the start time of the test in a session or DB. Anything stored via js, e.g. in a variable/object etc can be manipulated.

Also, will you be sending the data from a question when it is answered or store them and send them at the end?

This will determine how you address the server-side time checks.

Sorry . Forget to explain the flow . I want to nake the total timer for complete the quiz. Eg. When user click to answer the quiz. The timer set up for 5 minute will start countdown and after 5 mnute if they not submit. The time's up and the score appear.

Member Avatar for diafol

OK. When quiz delivered to user, store a timestamp in the DB / session var. Pass this to a js var in the page. Examples of various countdowns exist here: http://stackoverflow.com/questions/20618355/the-simplest-possible-javascript-countdown-timer

If you refresh the page on each new question (without ajax) then you need to reset the countdown (to the appropriate time left) on each page/question. WHen countdown reaches zero, it can lock the submit button or whatever you're using. BUT remember that this can be circumvented, so it is important that when the big submit occurs at the end, that you check the server time of the submit against the start time (stored in session or DB). Give the submit a bit of wiggle room, e.g. test takes 5 minutes until lockdown. User submits with milliseconds to go. Server needs some time to process the submission, so perhaps allow 5 minutes 5 seconds (perhaps not even that much) from start time on server to submission time. Hope that's clear?

hey, mr diafol , thank you for helping me . i'll try to make it and i will inform the result asap . seriously thank you . :)

i have added this code into my page but the timer does'nt appear

<body>
    <div>Quiz ends in <span id="time"></span> minutes!</div>
</body>
<script type="text/javascript">
window.onload = function() {

  var display = document.querySelector('#time'),
      timer = new CountDownTimer(5);

  timer.onTick(format).onTick(restart).start();

  function restart() {
    if (this.expired()) {
      setTimeout(function() { timer.start(); }, 1000);
    }
  }

  function format(minutes, seconds) {
    minutes = minutes < 10 ? "0" + minutes : minutes;
    seconds = seconds < 10 ? "0" + seconds : seconds;
    display.textContent = minutes + ':' + seconds;
  }
};
</script>

at quiz page it only show quiz end in minute.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.