javascript - 使用 document.getElementById 不适用于 php 表单

标签 javascript php jquery html

所以我这里有一个使用 javascript 的 php 计时器,我的问题是

即使我达到了 Total_Seconds 为 0 ,我的名为 quiz 的表单也不会自动提交,任何人都可以找到这里出了什么问题吗?

顺便说一句,我的代码没有错误。

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>

这是整个表单,不介意变量

<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 期望函数作为第一个参数,后面是时间延迟。 documentation

function delayAlert(){
   alert("setTimeout has been called");
}
setTimeout(delayAlert, 1000);

关于javascript - 使用 document.getElementById 不适用于 php 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33116745/

相关文章:

javascript - 动态地将面板添加到 Bootstrap 3 Accordion

javascript - 转换 Geojson 嵌套数组数据并将其打印为 jquery 中的列表

javascript - JSON.stringify 对于大对象来说非常慢

php - Symfony 2 命名空间错误仅在生产中出现(“尝试加载类...)

javascript - 如何显示/绘制以数字为输入的屏幕锁定图案

php - PHP 搜索建议中的排序结果

PHP ZipArchive::open 总是导致 ZIPARCHIVE::ER_READ 错误

javascript - 选中行时获取其他列数据的jQuery代码

javascript - SAP Cloud SDK for JavaScript 的 AttachmentService 问题

javascript - 如何使用(this)访问Angular 2 http rxjs catch函数中的对象属性