在PHP中为javascript设计静态计时器

时间:2018-03-04 10:27:52

标签: javascript



<?php
session_start();
error_reporting(1);
include("database.php");

extract($_POST);
extract($_GET);
extract($_SESSION);
/*$rs=mysql_query("select * from mst_que") or die(mysql_error());
if($_SESSION[qn]>mysql_num_rows($rs))
{
unset($_SESSION[qn]);
exit;
}*/
?>
<!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">
</head>

<body>

<?php
include("header.php");
extract($_SESSION);
$rs=mysql_query("select * from mst_res where
login='$login'",$cn) or die(mysql_error());

while($row=mysql_fetch_row($rs))
{
$tt="$row[3]";
}

if($tt=="1")
{
echo "You have already taken the test.Cannot take test again!";
exit;
}
?>








<?php


include("timer.php");



$query="select * from mst_que";


$rs=mysql_query("select * from mst_que") or die(mysql_error());


	
		if($submit=='Next Question' && isset($ans))
		{
				mysql_data_seek($rs,$_SESSION[qn]);
				$row= mysql_fetch_row($rs);
	
				mysql_query("insert into usr_ans(que_id,ques_desc,ans1,ans2,ans3,ans4,true_ans,your_ans) values ('$row[0]','$row[1]','$row[2]','$row[3]','$row[4]', '$row[5]','$row[6]','$ans')") or die(mysql_error());
				if($ans==$row[6])
				{
							$_SESSION[trueans]=$_SESSION[trueans]+1;
				}
				$_SESSION[qn]=$_SESSION[qn]+1;

		}
		else if($submit=='Get Result' && isset($ans))
		{
				mysql_data_seek($rs,$_SESSION[qn]);
				$row= mysql_fetch_row($rs);	
				mysql_query("insert into usr_ans(que_id,ques_desc,ans1,ans2,ans3,ans4,true_ans,your_ans) values ('$row[0]','$row[1]','$row[2]','$row[3]','$row[4]', '$row[5]','$row[6]','$ans')") or die(mysql_error());
				if($ans==$row[6])
				{
							$_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_res(login,test_date,score,test_taken) values('$login','".date("y/m/d")."',$_SESSION[trueans],1)") or die(mysql_error());
				echo "<h1 align=center><a href=index.php> Thank you for taking the online test</a> </h1>";
				unset($_SESSION[qn]);
		
				unset($_SESSION[trueans]);
				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>Question ".  $n .": $row[1]</style>";
echo "<tr><td class=style8><input type=radio name=ans value=1>$row[2]";
echo "<tr><td class=style8> <input type=radio name=ans value=2>$row[3]";
echo "<tr><td class=style8><input type=radio name=ans value=3>$row[4]";
echo "<tr><td class=style8><input type=radio name=ans value=4>$row[5]";

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>";
?>





</body>
</html>
&#13;
&#13;
&#13;

我目前正在开发PHP和HTML测验项目。 我从PHP数据库中检索问题,以便在浏览器中显示为选项。 我只是想实现一个简单的超时。我知道如何在计时器用完后创建一个计时器并将控制重定向到另一个页面,但由于问题是从数据库(PHP)中检索的,因此每次在浏览器上重新加载页面,每次发生这种情况时我的计时器重新启动(当紧接着下一个问题!)。 我想知道如何保持计时器运行,即使它穿过各种其他页面。

我用过的计时器代码:

&#13;
&#13;
<?php
$dateFormat = "d F Y -- g:i a";
$targetDate = time() + (01*60);//Change the 25 to however many minutes you want to countdown
$actualDate = time();
$secondsDiff = $targetDate - $actualDate;
$remainingDay     = floor($secondsDiff/60/60/24);
$remainingHour    = floor(($secondsDiff-($remainingDay*60*60*24))/60/60);
$remainingMinutes = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))/60);
$remainingSeconds = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))-($remainingMinutes*60));
$actualDateDisplay = date($dateFormat,$actualDate);
$targetDateDisplay = date($dateFormat,$targetDate);
?>



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Quiz System</title>

<script type="text/javascript">


  var days = <?php echo $remainingDay; ?>  
  var hours = <?php echo $remainingHour; ?>  
  var minutes = <?php echo $remainingMinutes; ?>  
  var seconds = <?php echo $remainingSeconds; ?> 
function setCountDown ()
{
  seconds--;
  if (seconds < 0){
      minutes--;
      seconds = 59
  }
  if (minutes < 0){
      hours--;
      minutes = 00
  }
  if (hours < 0){
      days--;
      hours = 00
  }
  document.getElementById("remain").innerHTML = hours+" hours, "+minutes+" minutes, "+seconds+" seconds";
  SD=window.setTimeout( "setCountDown()", 1000 );
  if (minutes == '00' && seconds == '00') { seconds = "00"; window.clearTimeout(SD);
   		//window.alert("Time is up. Press OK to continue."); // change timeout message as required
  		window.location = "http://localhost:8080/LL_project/timeup.php" // Add your redirect url
 	} 

}

</script>
</head>
<body onload="setCountDown();">

 <div id="remain"><?php echo "$remainingHour hours, $remainingMinutes minutes, $remainingSeconds seconds";?></div>

   
</body>
</html>
&#13;
&#13;
&#13;

0 个答案:

没有答案