Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

how to submit the form with timer

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

how to submit the form with timer

Postby kotturunarasimharao » Fri Mar 08, 2013 3:15 am

I am writing a simple javascript with timer after complection of time form will automatically submit and score will generated problem is form will not submited after complection of time
Code: Select all
<?php
session_start();
$minutes = 1;
$seconds = 0;
$time_limit = ($minutes * 60) + $seconds + 1;
if(!isset($_SESSION["start_time"])){$_SESSION["start_time"] = mktime(date(G),date(i),date(s),date(m),date(d),date(Y)) + $time_limit;} // Add $time_limit (total time) to start time. And store into session variable.
?>
<html>
<head>
<style>
#txt {
border:2px solid red;
font-family:verdana;
font-size:16pt;
font-weight:bold;
background: #FECFC7;
width:70px;
text-align:center;
color:#000000;
}
</style>
</head>
<body>
<input id="txt" readonly>
<script>
var ct = setInterval("calculate_time()",100);
function calculate_time()
{
 var end_time = "<?php echo $_SESSION["start_time"]; ?>";
 var dt = new Date();
 var time_stamp = dt.getTime()/1000;
 var total_time = end_time - Math.round(time_stamp);
 var mins = Math.floor(total_time / 60);
 var secs = total_time - (mins * 60);
 if(secs < 10){secs = "0" + secs;}
 document.getElementById("txt").value = mins + ":" + secs;
 if(mins <= 0)
 {
  if(secs <= 0 || mins < 0)
  {
   clearInterval(ct);
   document.getElementById("txt").value = "0:00";
   document.forms.f1.submit();
   alert("The time is up.");
   }
  }
 }
</script>
<?php
include("connection.php");

        $display = mysql_query("SELECT * FROM tbl_questions ORDER BY Qno ");

       if (!@$_POST['submit']) {

            echo "<form method=post action=\" \" name=f1 id=f1>";
            echo "<table border=0>";

            while ($row = mysql_fetch_array($display))
         {

            $id = $row["Qno"];
            $question = $row["question"];
            $opt1 = $row["option1"];
            $opt2 = $row["option2"];
            $opt3 = $row["option3"];
            $opt4 = $row["option4"];
            $answer = $row["answer"];

            echo "<tr><td colspan=3><br><b>$id&nbsp&nbsp$question</b></td></tr>";
            echo "<tr><td><input type=radio name=q$id value=\"$opt1\">$opt1</td><td> <input type=radio name=q$id value=\"$opt2\">$opt2</td><td> <input type=radio name=q$id value=\"$opt3\">$opt3</td><td> <input type=radio name=q$id value=\"$opt4\">$opt4</td></tr>";

}

            echo "</table>";
            echo "<input type='submit' value='submit' name='submit'>";
            echo "</form>";
        }

        else if($_POST['submit'])
      {
    
//echo"executed";
$score = 0;
$total = mysql_num_rows($display);

while ($result = mysql_fetch_array($display)) {

$answer = $result["answer"];
$id = $result["Qno"];
$or=q.$id;
//echo"$or";

if ($_POST[$or] == $answer) {
$score++;
}

}

echo "<p align=center><b>You scored $score out of $total</b></p>";
}


?>

</body>
</html>
kotturunarasimharao
New php-forum User
New php-forum User
 
Posts: 1
Joined: Thu Mar 07, 2013 8:57 pm

Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 3 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.