我想创建一个简单的测验站点,用户可以在指定的时间(例如5秒钟)内在该时间之后自动进行测验。如果用户在5个问题中填写了2个问题,那么在倒计时结束后,只有这2个问题将被提交到数据库中。另外,我想在单击提交按钮时存储倒数。例如,如果用户在倒计时时间为4秒时提交表单,则该4秒时间将存储在mySQL数据库中。倒数计时器并在计时器结束后自动提交,但数据库中没有存储任何数据。我的代码:
<form action="form_data.php" method="post" class="form" id="form" name="form">
<div class="col-md-12">
<span id="remain"></span>
<h2 class="text-center">Name of the President?</h2>
<div class="row">
<div class="col-md-4 ">
<div class="form-check vote">
<label class="form-check-label"><input type="radio" class="form-check-input " name="president" value="1" required>A</label>
</div>
</div>
<div class="col-md-4 ">
<div class="form-check vote">
<label class="form-check-label"><input type="radio" class="form-check-input " name="president" value="2" required>B</label>
</div>
</div>
</div>
<div class="row vote_submit">
<div class="col-md-6">
<input type="submit" name="go" class="form-control" value="Submit" />
</div>
<div class="col-md-6">
<input type="reset" class="form-control btn btn-danger" value="Reset">
</div>
</div>
</div>
</form>
<script type="text/javascript">
window.onload=counter;
function counter()
{
seconds = 5;
countDown();
}
function countDown(){
document.getElementById("remain").innerHTML=seconds;
setTimeout("countDown()",1000);
if(seconds == 0)
{
document.form.submit();
}else {
seconds--;
}
}
</script>
form_data.php文件:
<?php
if(isset($_POST["go"])){
//Database Connection Establish
include "dbconnect.php";
//Variables for Form Data
$president = $DBcon->real_escape_string($_POST['president']);
$dataTime = date("Y-m-d H:i:s");
//Insert Form Data into database
$insert = $DBcon->query("INSERT into vote (presidentcreated) VALUES ('$president','$dataTime')");
if($insert){
echo "<h1>Thank You for Your vote</h1>";
}else{
echo "Error".mysqli_error($DBcon);
}
}
?>