[英]when Countdown timer = 0, alert button or submit form
When my timer count to 00:00:00
alert('Get Result')
button or submit the form当我的计时器计数到
00:00:00
alert('Get Result')
按钮或提交表单时
I cant even alert message in response.php.我什至无法在 response.php 中发出警报消息。
This is Where I select the duration from database这是我从数据库中选择持续时间的地方
while($row=mysqli_fetch_array($res))
{
$duration=$row["duration"];
}
$_SESSION["duration"]=$duration;
$_SESSION["start_time"]=date("Y-m-d H:i:s");
$end_time=$end_time=date('Y-m-d H:i:s', strtotime('+'.$_SESSION["duration"].'minutes',strtotime($_SESSION["start_time"])));
$_SESSION["end_time"]=$end_time;
This is response.php这是 response.php
<?php
session_start();
$from_time1=date('Y-m-d H:i:s');
$to_time1=$_SESSION["end_time"];
$timefirst=strtotime($from_time1);
$timesecond=strtotime($to_time1);
$differenceinseconds=$timesecond-$timefirst;
if($differenceinseconds<0){
// This is for when timer smaller then 0 then = 00:00:00
$differenceinseconds=0;
echo "TIME UP<br>";
//I try to alert a simple message here, and dint work. Why is this happen
}
echo gmdate("H:i:s",$differenceinseconds);
?>
Here is the javascript in quiz page这是测验页面中的javascript
<script type="text/javascript">
var x =setInterval(test,1000);
function test()
{
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","response.php",false);
xmlhttp.send(null);
document.getElementById("response").innerHTML=xmlhttp.responseText;
}
</script>
The display timer tag显示计时器标签
<div id=response class=timer style=font-size:30px></div>
The Form name and the button表单名称和按钮
<form name=myfm method=post action=quizz.php>
<input type=submit name=submit value='Get Result'>
Your PHP should only get the difference in time.您的 PHP 应该只获得时间上的差异。 That means that PHP will always output a format of
HH:mm:ss
, and no other text or values, which you then get in JavaScript.这意味着 PHP 将始终输出
HH:mm:ss
格式,而不会输出其他文本或值,然后您将在 JavaScript 中获得这些文本或值。 Ensuring that the output is always the same, unless you're using encoded arrays, means that you can design your code to expect the values you always send.确保输出始终相同,除非您使用编码数组,这意味着您可以设计您的代码以期望您始终发送的值。
$differenceinseconds = $timesecond - $timefirst;
if ($differenceinseconds < 0){
$differenceinseconds = 0;
}
echo gmdate("H:i:s", $differenceinseconds);
You can then check that value after you fetch the values in JavaScript, since you now know that the only thing printed by your response.php
is a time in the format of HH:mm:sss
.然后,您可以在 JavaScript 中获取值后检查该值,因为您现在知道
response.php
打印的唯一内容是HH:mm:sss
格式的时间。
function test() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "response.php", false);
xmlhttp.send(null);
var response = xmlhttp.responseText;
document.getElementById("response").innerHTML = response;
if (response == "00:00:00") {
alert("Time's up!");
}
}
If you want to submit the form as well, add a submit()
within the condition if (response == "00:00:00") {
.如果您还想提交表单,请在条件
if (response == "00:00:00") {
添加submit()
。
document.getElementsByName('myfm')[0].submit();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.