[英]Get / replace contents of div tag using PHP and Ajax
I'm very new to PHP/Ajax/Html so here's my baby problem (I'm making a radio station site), 我是PHP / Ajax / Html的新手,所以这是我的宝贝问题(我正在建立一个广播电台站点),
test.php queries my server for the currently playing song name. test.php向我的服务器查询当前播放的歌曲名称。
listen.php displays this song name in the 'refreshTitle' div tag. listen.php在'refreshTitle'div标签中显示此歌曲名称。
When my station changes song there is a 30-ish second delay, so what I want to do is get the song title every second and compare / delay the update of the display if the title is different to what is actually being heard, easy peasy right?! 当我的电台更改歌曲时,会有30秒钟的延迟,所以我想做的就是每秒获取歌曲标题,并比较/延迟显示的更新(如果标题与实际听到的不同),轻松自在对?! Here is listen.php :
这是listen.php :
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
function get_XmlHttp() {
// create the variable that will contain the instance of the XMLHttpRequest object (initially with null value)
var xmlHttp = null;
if (window.XMLHttpRequest) { // for Firefox, IE7+, Opera, Safari, ...
xmlHttp = new XMLHttpRequest();
}
else if (window.ActiveXObject) { // for Internet Explorer 5 or 6
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlHttp;
}
function ajaxrequest(php_file, tagID) {
var request = get_XmlHttp(); // call the function for the XMLHttpRequest instance
// create pairs index=value with data that must be sent to server
var d = new Date();
var t = d.toLocaleTimeString();
var the_data = 'test=' + t;
//append time purely for testing to make sure text updates
request.open("POST", php_file, true); // set the request
// adds a header to tell the PHP script to recognize the data as is sent via POST
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.send(the_data); // calls the send() method with datas as parameter
// Check request status
// If the response is received completely, will be transferred to the HTML tag with tagID
request.onreadystatechange = function () {
if (request.readyState == 4) {
document.getElementById(tagID).innerHTML = request.responseText;
}
}
}
setInterval(
function () {
ajaxrequest('test.php', 'refreshTitle')
}, 1000); // refresh every 10000 milliseconds
//This time val should vary based on the song title retrieved during the last second
</script>
And somewhere down the rest of the page I have this: 在页面其余部分的下方,我有这个:
echo "<div id=\"refreshTitle\" style=\"float:left; padding-top:6px;\">\n";
echo "</div>\n";
In test.php (the file with the song name) I have this: 在test.php (带有歌曲名称的文件)中,我有以下内容:
if (isset($_POST['test'])) {
$str = $_POST['test']; // get data
echo $dnas_data['SONGTITLE'] . " Current time " . $str;
}
So basically every second I send the time to test.php , which echos it and that echo I assume is put into 'refreshTitle' with this line: 因此,基本上我每秒都会将时间发送到test.php ,它会对其进行回显,并将我认为的回显与此行一起放入“ refreshTitle”:
document.getElementById(tagID).innerHTML = request.responseText;
What I want to do is get the song title into my javascript in listen.php and only run the ajax request after some string comparison / delay logic. 我想做的是将歌曲标题输入到listen.php中的 javascript中,并且仅在经过一些字符串比较/延迟逻辑后才运行ajax请求。
Sorry for the long-winded description but I'm fairly confused and think I've done this whole thing backwards :) Please let me know any thoughts... 很抱歉,冗长的描述,但是我很困惑,认为我已经把整个事情都做了倒退:)请让我知道任何想法...
First, I would recommend to you to update your jQuery version (1.3 is kinda old, the current version is 2+). 首先,我建议您更新jQuery版本(1.3有点旧,当前版本是2+)。
What's more, AJAX requesting is very well abstracted in jQuery : you can use the load() function to simply load an element content. 而且,AJAX请求在jQuery中非常抽象:您可以使用load()函数简单地加载元素内容。
Source : http://api.jquery.com/load/ 资料来源: http : //api.jquery.com/load/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.