So I have here a php timer using javascript, my problem is
this line when even I reached total_seconds to 0 , my form which is named quiz is not auto submitting, anyone can locate whats wrong here?
btw there is no error with my code.
if(total_seconds <=0){setTimeout('document.getElementById("quiz").submit()',1);
<div style="font-weight: bold" id="quiz-time-left">
<script type="text/javascript">
var max_time = <?php echo $total_timer1 ?>*60;
var c_seconds = 0;
var total_seconds =max_time;
max_time = parseInt(total_seconds/60);
c_seconds = parseInt(total_seconds%60);
document.getElementById("quiz-time-left").innerHTML='Time Left: ' + max_time + ' minutes ' + c_seconds + ' seconds';
function init(){
document.getElementById("quiz-time-left").innerHTML='Time Left: ' + max_time + ' minutes ' + c_seconds + ' seconds';
setTimeout("CheckTime()",999);
}
function CheckTime(){
document.getElementById("quiz-time-left").innerHTML='Time Left: ' + max_time + ' minutes ' + c_seconds + ' seconds' ;
if(total_seconds <=0){
setTimeout('document.getElementById("quiz").submit()',1); <-- **my problem**
} else
{
total_seconds = total_seconds -1;
max_time = parseInt(total_seconds/60);
c_seconds = parseInt(total_seconds%60);
setTimeout("CheckTime()",999);
}
}
init();
</script>
</div>
Here is the whole form dont mind the variables
<form action="result.php" method="post" id='quiz'>
<?php
$username=$_SESSION['username'];
$query1 = mysql_query("SELECT * FROM student WHERE username='$username'");
$row1 = mysql_fetch_assoc($query1);
$id1 = $row1['id'];
$query = mysql_query("SELECT * FROM timer WHERE s_id='$id1' AND q_id='$id' ");
$row = mysql_fetch_assoc($query);
$currenttime = date ("h:i:sa");
$q_start10 = strtotime ($currenttime);
$q_time = $row['q_time'];
$q_endtime = $row['q_endtime'];
$total_timer = $q_endtime - $q_start10;
$total_timer1 = $total_timer / 60;
?>
<font size="6">
<div style="font-weight: bold" id="quiz-time-left"></div>
<script type="text/javascript">
var max_time = <?php echo $total_timer1 ?>*60;
var c_seconds = 0;
var total_seconds =max_time;
max_time = parseInt(total_seconds/60);
c_seconds = parseInt(total_seconds%60);
document.getElementById("quiz-time-left").innerHTML='Time Left: ' + max_time + ' minutes ' + c_seconds + ' seconds';
function init(){
document.getElementById("quiz-time-left").innerHTML='Time Left: ' + max_time + ' minutes ' + c_seconds + ' seconds';
setTimeout("CheckTime()",999);
}
function CheckTime(){
document.getElementById("quiz-time-left").innerHTML='Time Left: ' + max_time + ' minutes ' + c_seconds + ' seconds' ;
if(total_seconds <=0){
setTimeout(function(){document.getElementById("quiz").submit()},1000)
} else
{
total_seconds = total_seconds -1;
max_time = parseInt(total_seconds/60);
c_seconds = parseInt(total_seconds%60);
setTimeout("CheckTime()",999);
}
}
init();
</script>
</font>
<br>
<br>
<?php
$score = 0;
$tbl_name2="a_quiz"; // Switch to table "forum_answer"
$sql2="SELECT * FROM $tbl_name2 WHERE q_id='$id'";
$result2=mysql_query($sql2);
while($rows=mysql_fetch_array($result2)){
$q_question = $rows['q_question'];
?>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr style='overflow:hidden; word-wrap:break-word;'>
<p hidden>
<input type="text" name="q_id" value="<?php echo $rows['q_id'];?>">
</p>
<td bgcolor="lightgreen"><strong>Question:</strong></td>
<td bgcolor="lightgreen">:</td>
<td bgcolor="lightgreen" style="max-width: 1000px;"><?php echo $rows['q_question']; ?></td>
</tr>
<tr>
<td bgcolor="lightgreen"><strong>A</strong></td>
<td bgcolor="lightgreen">:</td>
<td bgcolor="lightgreen"><input type="radio" name="<?php echo $rows['a_id']; ?>" value="a"><?php echo $rows['a'] ?></td>
</tr>
<tr>
<td width="18%" bgcolor="lightgreen"><strong>B</strong></td>
<td width="5%" bgcolor="lightgreen">:</td>
<td width="77%" bgcolor="lightgreen"><input type="radio" name="<?php echo $rows['a_id']; ?>" value="b"><?php echo $rows['b'] ?></td>
</tr>
<tr>
<td width="18%" bgcolor="lightgreen"><strong>C</strong></td>
<td width="5%" bgcolor="lightgreen">:</td>
<td width="77%" bgcolor="lightgreen"><input type="radio" name="<?php echo $rows['a_id']; ?>" value="c"><?php echo $rows['c'] ?></td>
</tr>
<tr>
<td bgcolor="lightgreen"><strong>D</strong></td>
<td bgcolor="lightgreen">:</td>
<td bgcolor="lightgreen"><input type="radio" name="<?php echo $rows['a_id']; ?>" value="d"><?php echo $rows['d'] ?></td>
</tr>
</table></td>
</tr>
<hr>
</table><br>
<?php
}
?>
<?php
$connection=mysql_connect('localhost', 'root','');
mysql_select_db('thesis');
$username= $_SESSION['username'];
$query6 = mysql_query("SELECT * FROM student WHERE username='$username'");
$row6 = mysql_fetch_assoc($query6);
$s_id = $row6['id'];
$fname = $row6['f_name'];
$mname = $row6['m_name'];
$lname = $row6['l_name'];
$email = $row6['email'];
$position = $row6['position'];
?>
<p hidden>
<input type="text" name="s_id" value="<?php echo $s_id ?>">
</p>
<input type="submit" name="submit" value="Submit Answer" class="btn">
</form>
setTimeout expects function as a first parameter and followed with the time delay. documentation
function delayAlert(){ alert("setTimeout has been called"); } setTimeout(delayAlert, 1000);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.