我无法在html中显示来自PHP脚本的输出以进行倒数计时器。所需的时间是通过输入。
<div id="countdown"> <!-- Display input for the kitchen timer -->
<form method="post" action="display_timer()">
<br>
<br>
<h1>Enter the hours required: <input type="text" name="hours"><br></h1>
<h2>Enter the Minutes required: <input type="text" name="minutes"><br></h2>
<h3>Enter the Seconds required: <input type="text" name="seconds"><br></p>
<br>
<br>
<h4><input type="submit" name="Submit" value="Submit"></h4>
</form>
</div>
从带有ajax
的php脚本调用function display_timer(){
var myAjax9 = new XMLHttpRequest;
myAjax9.open("GET", "timer.php");
myAjax9.onreadystatechange = function(){
if (myAjax9.readyState == 4 && myAjax7.status == 200){
document.getElementById("timerdisplay").innerHTML = hours+" hours, "+minutes+" minutes, "+seconds+" seconds";
IT=window.setTimeout( "setcountDown()", 10 );
if (hours == '00' && minutes == '00' && seconds == '00') {
seconds = "00";
window.clearTimeout(IT);
window.alert("Time is up. Press OK to continue.");
}
}
}
}
并以
显示 <div id="timerdisplay"> <?php echo "$hoursLeft hours, $minutesLeft minutes, $secondsLeft seconds";?></div> <!-- Display output for the kitchen timer -->
如果需要,我可以打开计时器PHP代码。
我是php和Ajax的新手,所以可能会做一些无可救药的错误吗?
答案 0 :(得分:0)
如果您需要将时间数据发送给PHP,则需要解决此问题:
action="display_timer()"
的{{1}},并在onsubmit="return display_timer()"
的最后一行添加return false
函数(这将取消提交,因此您可以使用ajax)。此外,如果要检索您的数据,请抛出ajax调用,然后您的display_timer
应该发送类似这样的内容(JSON表达式):
timer.php
然后,您的<?php echo "{\"hours\":\"$hoursLeft\", \"minutes\":\"$minutesLeft\", \"seconds\":\"$secondsLeft\"}";?>
功能将是:
display_timer