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 –