所以我有一个倒数计时器,当时钟达到0时抓取.php文件。唯一的问题是,php文件在新窗口中打开。我希望以某种方式显示在页面底部。
我对javascript或AJAX一无所知。只有php和html。任何人都可以向新手展示该怎么做?
<!--START COUNTDOWN TIMER SCRIPT--> <br> <script type="text/javascript"> window.onload = function() { countDown('my_div1', 'timerunout.php', 4); } function countDown(elID, output, seconds) { var elem = document.getElementById(elID), start = new Date().getTime(), end = start+seconds*1000, timer = setInterval(function() { var now = new Date().getTime(), timeleft = end-now, timeparts; if( timeleft < 0) { document.location.href = output; clearInterval(timer); } else { timeparts = [Math.floor(timeleft/60000),Math.floor(timeleft/1000)%60]; if( timeparts[1] < 10) timeparts[1] = "0"+timeparts[1]; elem.innerHTML = "When the Clock hits Zero...<br> "+timeparts[0]+":"+timeparts[1]; } },250); // the lower this number, the more accurate the timer. 250 recommended } </script> <center> <font color="#FF0000"><b><h1> <div id="my_div1"> </div> </h1></b></font> </center> <!--END COUNTDOWN TIMER SCRIPT-->
好的,所以我在index.php上有一个倒数计时器,当它达到0时...它会使用AJAX在页面底部打开另一个php文件。我们称之为timerunout.php
问题是这个......因为Php文件是一个链接。 在该链接的末尾需要附加到其末尾的联盟ID。
仅当有人在index.php中的地址末尾键入其用户名时才会检测到该联属会员ID。有什么建议吗?
这是index.php的代码
window.onload = function(){ countDown('my_div1','timerunout.php',4); }function countDown(elID, output, seconds) { var elem = document.getElementById(elID); start = new Date().getTime(), end = start+seconds*1000, timer = setInterval(function() { var now = new Date().getTime(), timeleft = end-now, timeparts; if( timeleft < 0) { //This code creates the AJAX object, which will then be used to send it. var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } //This code parameterizes the object to point at your page in an asynchronous way. xmlhttp.open("GET","timerunout.php?s1=username",true); xmlhttp.send(); //Once all parameters are set, send it on it's way. //Callback. Once the request is in one of it's stages, this will be called. xmlhttp.onreadystatechange=function() { //Request done and fetching the page successful? if (xmlhttp.readyState==4 && xmlhttp.status==200) { //Sets the HTML of my_div1 to whatever was in timerunout.php elem.innerHTML=xmlhttp.responseText; } } clearInterval(timer); } else { timeparts = [Math.floor(timeleft/60000),Math.floor(timeleft/1000)%60]; if( timeparts[1] < 10) timeparts[1] = "0"+timeparts[1]; elem.innerHTML = "When the Clock hits Zero...<br> "+timeparts[0]+":"+timeparts[1]; } } ,250); // the lower this number, the more accurate the timer. 250 recommended } </script> <center> <div id="my_div1"></div> </center> <!--END COUNTDOWN TIMER SCRIPT-->
这里是timerunout.php
// // Maybe check $s1 is indeed valid // $newurl = sprintf('/join.php?id=%s', urlencode($_GET['s1'])); $coaching = sprintf('/live/webinar-register.php?id=%s',
进行urlencode($ _ GET [ 'S1'])); ?&GT;
基本上我所说的是在index.php中,
此行中的username
需要来自用户在地址栏中输入的内容。
xmlhttp.open("GET","timerunout.php?s1=username",true);
答案 0 :(得分:2)
如果你想使用jQuery-Framework,你可能会遇到这样的问题:
$.ajax({
url: "timerunout.php",
data:{ s1:"whateverparametersuitsyourfancy" }
}).done(function(data) {
$('#my_div1').html(data);
});
如果你想采用传统方式,AJAX调用会更复杂一些。来自w3schools网站:
//This code creates the AJAX object, which will then be used to send it.
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
//This code parameterizes the object to point at your page in an asynchronous way.
xmlhttp.open("GET","timerunout.php?s1=whateverparametersuitsyourfancy",true);
xmlhttp.send(); //Once all parameters are set, send it on it's way.
//Callback. Once the request is in one of it's stages, this will be called.
xmlhttp.onreadystatechange=function() {
//Request done and fetching the page successful?
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
//Sets the HTML of my_div1 to whatever was in timerunout.php
document.getElementById("my_div1").innerHTML=xmlhttp.responseText;
}
}
两个代码都会向相关页面发送GET请求,并将目标页面的“回显”输出写入由'my_div1'标识的html标记。
在你的代码中(我将使用普通的JS示例),这可能看起来像这样。
<!--START COUNTDOWN TIMER SCRIPT-->
<br>
<script type="text/javascript">
window.onload = function() {
countDown('my_div1', 'timerunout.php', 4);
}
function countDown(elID, output, seconds) {
var elem = document.getElementById(elID);
start = new Date().getTime(), end = start+seconds*1000,
timer = setInterval(function() {
var now = new Date().getTime(), timeleft = end-now, timeparts;
if( timeleft < 0) {
//This code creates the AJAX object, which will then be used to send it.
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
//This code parameterizes the object to point at your page in an asynchronous way.
xmlhttp.open("GET","timerunout.php?s1=whateverparametersuitsyourfancy",true);
xmlhttp.send(); //Once all parameters are set, send it on it's way.
//Callback. Once the request is in one of it's stages, this will be called.
xmlhttp.onreadystatechange=function() {
//Request done and fetching the page successful?
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
//Sets the HTML of my_div1 to whatever was in timerunout.php
elem.innerHTML=xmlhttp.responseText;
}
}
clearInterval(timer);
} else {
timeparts = [Math.floor(timeleft/60000),Math.floor(timeleft/1000)%60];
if( timeparts[1] < 10) timeparts[1] = "0"+timeparts[1];
elem.innerHTML = "When the Clock hits Zero...<br> "+timeparts[0]+":"+timeparts[1];
}
} ,250); // the lower this number, the more accurate the timer. 250 recommended
}
</script>
<center>
<font color="#FF0000"><b><h1>
<div id="my_div1"></div>
</h1></b></font>
</center>
<!--END COUNTDOWN TIMER SCRIPT-->
编辑:我在AJAX调用中添加了一个GET参数,以方便您使用。