AJAX 動(dòng)態(tài)獲取當(dāng)前時(shí)間(php)
更新時(shí)間:2009年11月27日 16:13:19 作者:
利用AJAX動(dòng)態(tài)獲取當(dāng)前時(shí)間,客戶端time.php,服務(wù)器端time_check.php
客戶端代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>動(dòng)態(tài)顯示時(shí)間</title>
</head>
<script language="javascript">
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
else{
alert("創(chuàng)建請(qǐng)求失敗");
}
}
function sendRequest(){
createXMLHttpRequest();
url = "time_check.php";
xmlHttp.onreadystatechange = callback;
xmlHttp.open('GET',url,true);
xmlHttp.send(null);
}
function callback(){
if(xmlHttp.readyState ==4){
if(xmlHttp.status == 200){
document.getElementById("time").innerHTML = xmlHttp.responseText;
setTimeout("sendRequest()",1000);
}
}
}
</script>
<body>
<input type="button" value="check it" onclick="sendRequest();" />
<br/>
<span id="time"></span>
</body>
</html>
服務(wù)器端代碼:
<?php
header("cache-control:no-cache,must-revalidate"); //取消php頁面緩存的設(shè)置
header('Content-type: text/html;charset=GB2312');
$showtime = date("北京時(shí)間Y年m月d日H:i:s");
echo $showtime;
?>
復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>動(dòng)態(tài)顯示時(shí)間</title>
</head>
<script language="javascript">
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
else{
alert("創(chuàng)建請(qǐng)求失敗");
}
}
function sendRequest(){
createXMLHttpRequest();
url = "time_check.php";
xmlHttp.onreadystatechange = callback;
xmlHttp.open('GET',url,true);
xmlHttp.send(null);
}
function callback(){
if(xmlHttp.readyState ==4){
if(xmlHttp.status == 200){
document.getElementById("time").innerHTML = xmlHttp.responseText;
setTimeout("sendRequest()",1000);
}
}
}
</script>
<body>
<input type="button" value="check it" onclick="sendRequest();" />
<br/>
<span id="time"></span>
</body>
</html>
服務(wù)器端代碼:
復(fù)制代碼 代碼如下:
<?php
header("cache-control:no-cache,must-revalidate"); //取消php頁面緩存的設(shè)置
header('Content-type: text/html;charset=GB2312');
$showtime = date("北京時(shí)間Y年m月d日H:i:s");
echo $showtime;
?>
您可能感興趣的文章:
- PHP 利用AJAX獲取網(wǎng)頁并輸出的實(shí)現(xiàn)代碼(Zjmainstay)
- ajax獲取php頁面的返回參數(shù),控件賦值的方法
- 用PHP獲取Google AJAX Search API 數(shù)據(jù)的代碼
- PHP Ajax JavaScript Json獲取天氣信息實(shí)現(xiàn)代碼
- php獲取ajax的headers方法與內(nèi)容實(shí)例
- php+ajax實(shí)現(xiàn)無刷新動(dòng)態(tài)加載數(shù)據(jù)技術(shù)
- php基于jquery的ajax技術(shù)傳遞json數(shù)據(jù)簡單實(shí)例
- ajax處理php返回json數(shù)據(jù)的實(shí)例代碼
- Ajax+php數(shù)據(jù)交互并且局部刷新頁面的實(shí)現(xiàn)詳解
- PHP+ajax實(shí)現(xiàn)獲取新聞數(shù)據(jù)簡單示例
相關(guān)文章
js對(duì)ajax返回?cái)?shù)組的處理介紹
本篇文章主要是對(duì)js對(duì)ajax返回?cái)?shù)組的處理進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-02-02解決ajax跨域請(qǐng)求數(shù)據(jù)cookie丟失問題
本文主要是從前端jquery和服務(wù)端php為例,分別使用實(shí)例解決ajax跨域請(qǐng)求數(shù)據(jù)cookie丟失問題,推薦給有相同需求的小伙伴們。2015-03-03jQuery中ajax - post() 方法實(shí)例詳解
在jquery中的ajax有二個(gè)數(shù)據(jù)發(fā)送模式,一種是get,另一種是post(),下面我來給大家介紹介紹,有需要了解的朋友可參考2015-09-09ajax與websocket的區(qū)別以及websocket常用使用方式 介紹
這篇文章主要介紹了ajax與websocket的區(qū)別以及websocket常用使用方式 介紹,需要的朋友可以參考下2018-06-06