Timer in php

i’m creating a new site for quiz, It show timer for every question, but when timer stops it stucks on same question. I want to show next question when timer ends.
My Script is:

    <?php
session_start();
error_reporting(1);
include("database.php");
extract($_POST);
extract($_GET);
extract($_SESSION);
if(isset($subid) && isset($testid))
{
$_SESSION[sid]=$subid;
$_SESSION[tid]=$testid;
header("location:quiz.php");
}
if(!isset($_SESSION[sid]) || !isset($_SESSION[tid]))
{
	header("location: index.php");
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Online Quiz</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="quiz.css" rel="stylesheet" type="text/css">
<style>
.timer {
	margin-left:40px;
	padding:5px;
	border-radius:5px;
	border:1px solid red;
	color:red;
	width:80px;
	
}

</style>

</head>

<body>

<?php
include("header.php");

echo '<div class="timer"><b><div id="countdown"></div>
<div id="notifier"></div></b></div>';
$query="select * from mst_question";

$rs=mysql_query("select * from mst_question where test_id=$tid",$cn) or die(mysql_error());
if(!isset($_SESSION[qn]))
{
	$_SESSION[qn]=0;
	mysql_query("delete from mst_useranswer where sess_id='" . session_id() ."'") or die(mysql_error());
	$_SESSION[trueans]=0;
	
}
else
{	
		if($submit=='Next Question')
		{
				mysql_data_seek($rs,$_SESSION[qn]);
				$row= mysql_fetch_row($rs);	
				mysql_query("insert into mst_useranswer(sess_id, test_id, que_des, ans1,ans2,ans3,ans4,true_ans,your_ans) values ('".session_id()."', $tid,'$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$ans')") or die(mysql_error());
				if($ans==$row[7])
				{
							$_SESSION[trueans]=$_SESSION[trueans]+1;
				}
				$_SESSION[qn]=$_SESSION[qn]+1;
		}
		else if($submit=='Get Result')
		{
				mysql_data_seek($rs,$_SESSION[qn]);
				$row= mysql_fetch_row($rs);	
				mysql_query("insert into mst_useranswer(sess_id, test_id, que_des, ans1,ans2,ans3,ans4,true_ans,your_ans) values ('".session_id()."', $tid,'$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$ans')") or die(mysql_error());
				if($ans==$row[7])
				{
							$_SESSION[trueans]=$_SESSION[trueans]+1;
				}
				echo "<h1 class=head1> Result</h1>";
				$_SESSION[qn]=$_SESSION[qn]+1;
				echo "<Table align=center><tr class=tot><td>Total Question<td> $_SESSION[qn]";
				echo "<tr class=tans><td>True Answer<td>".$_SESSION[trueans];
				$w=$_SESSION[qn]-$_SESSION[trueans];
				echo "<tr class=fans><td>Wrong Answer<td> ". $w;
				echo "</table>";
				mysql_query("insert into mst_result(login,test_id,test_date,score) values('$login',$tid,'".date("d/m/Y")."',$_SESSION[trueans])") or die(mysql_error());
				echo "<h1 align=center><a href=review.php> Review Question</a> </h1>";
				unset($_SESSION[qn]);
				unset($_SESSION[sid]);
				unset($_SESSION[tid]);
				unset($_SESSION[trueans]);
				exit;
		}
}
$rs=mysql_query("select * from mst_question where test_id=$tid",$cn) or die(mysql_error());
if($_SESSION[qn]>mysql_num_rows($rs)-1)
{
unset($_SESSION[qn]);
echo "<h1 class=head1>Some Error  Occured</h1>";
session_destroy();
echo "Please <a href=index.php> Start Again</a>";

exit;
}

mysql_data_seek($rs,$_SESSION[qn]);
$row= mysql_fetch_row($rs);

echo "<form name=myfm method=post action=quiz.php>";
echo "<table width=100%> <tr> <td width=30>&nbsp;<td> <table border=0>";
$n=$_SESSION[qn]+1;
echo "<tR><td><span class=style2>Que ".  $n .": $row[2]</style>";
echo "<tr><td class=style8><input type=radio name=ans value=1>$row[3]";
echo "<tr><td class=style8> <input type=radio name=ans value=2>$row[4]";
echo "<tr><td class=style8><input type=radio name=ans value=3>$row[5]";
echo "<tr><td class=style8><input type=radio name=ans value=4>$row[6]";

if($_SESSION[qn]<mysql_num_rows($rs)-1)
echo "<tr><td><input type=submit name=submit value='Next Question'></form>";
else
echo "<tr><td><input type=submit name=submit value='Get Result'></form>";
echo "</table></table>";
?>


<script type="text/javascript">
(function () {
  function display( notifier, str ) {
    document.getElementById(notifier).innerHTML = str;
  }
 
  function toMinuteAndSecond( x ) {
    return Math.floor(x/60) + ":" + (x=x%60 < 1 ? 0 : x);
  }
 
  function setTimer( remain, actions ) {
    var action;
    (function countdown() {
       display("countdown", toMinuteAndSecond(remain));
       if (action = actions[remain]) {
         action();
       }
       if (remain > 0) {
         remain -= 1;
         setTimeout(arguments.callee, 1000);
       }
    })(); // End countdown
  }
 
  setTimer(45, {
     0: function () { display("notifier", "Time is up!");       }
  });
})();
 
</script>
</body>
</html>

You have done every possible thing wrong that you could do with this code. For starters, you are using dangerous, obsolete code that has been completely removed from Php. You need to use PDO with prepared statements. Start with that and then come back if you are still stuck.

Here is a tutorial to get you going.
https://phpdelusions.net/pdo

I don’t know php / pdo I’ve downloaded this script from internet.
I’ll be grateful to you if you help me in this project.

Thanks

Do you have any intention of learning it or do you just want someone to write the code for you?

1 Like

i need code. can i send my script to you, if you can make changes in that script?

Well, Akashormani, this is more complicated to explain.

First, you must understand PHP is a SERVER-SIDE system and can not handle timers easily.
Next, timers are usually created using Javascript which is a CLIENT-SIDE system.

Server-side means that the code runs on the server and the output of that code is sent to the browser.
Client-side means that the rest of the code is handled in the browser.
Most timers need to be inside the browser, not on the server.

Here is a tutorial on how to do this in Javascript. I think it is what you need and it walks you thru all of the parts you will need to create it. After you get it working, if you have any problems post them here and we can help you solve those. Hope this helps! Javascript-Quiz-Tutorial

Here’s a quiz I wrote using PHP, JSON and Vanilla Javascript that I did awhile back. You are free to use the code, modify it to your liking or just get some ideas on how to do it.

https://github.com/Strider64/Trivia-Game-with-vanilla-javascript-and-PHP

And there is your second problem. I looked at the code you PM’d me. It is dangerous obsolete code that will not work whatsoever in current Php. There is no “fixing” it. It would require a complete re-write from the ground up.

Sponsor our Newsletter | Privacy Policy | Terms of Service