div标签的内容,我很新的PHP/AJAX/HTML所以这里是我的宝贝的问题(我正在做一个电台网站),获取/替换使用PHP和Ajax
test.php的向我的服务器查询当前正在播放的歌曲名称。
listen.php在'refreshTitle'div标签中显示此歌曲名称。
当我的电台改变歌曲时,会有30秒的延迟,所以我想要做的是每秒获得歌曲标题,并比较/延迟显示器的更新,如果标题与实际不同听过,轻松愉快吧?!这里是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>
和向下的页面的其余部分的地方我有这样的:
echo "<div id=\"refreshTitle\" style=\"float:left; padding-top:6px;\">\n";
echo "</div>\n";
在test.php的(与歌曲文件名的文件)我有这样的:
if (isset($_POST['test'])) {
$str = $_POST['test']; // get data
echo $dnas_data['SONGTITLE'] . " Current time " . $str;
}
所以基本上每一秒我送的时间test.php的,这是回声和回声我认为投入这一行“refreshTitle”:
document.getElementById(tagID).innerHTML = request.responseText;
我想要做的就是歌名到我的javascript在listen.php,只有运行一些字符串比较/延迟逻辑后的ajax请求。
对不起,长篇大论的描述,但我相当困惑,认为我做这件事向后:)请让我知道什么想法......
啊好吧谢谢,我甚至没有注意到Ajax和JQuery的区别。所以从不同的php页面获取变量等,通过jQuery函数更容易完成,我基本上可以杀死大部分代码? –
这是我最终结束的一种解决方案的链接(感谢Steven)如果有其他新手感兴趣:))http://pastebin.com/kuVbxq1Y –