您的位置:首页 > 编程语言 > PHP开发

PHP AJAX

2015-07-10 11:17 543 查看
zidong.html

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>标题动态</title>
</head>
<body>
<h1>Ajax动态显示时间</h1>
<input type="button" value="开始显示时间" id="go"  />
当前时间:<font color="red"><span id="showtime"></span></font><br/>
当前时间:<font color="red"><span id="showtime2"></span></font>
</body>
<script type="text/javascript">
var xmlHttp;
var goID=document.getElementById("go");
goID.onclick=function(){
start();
}
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
}
function start(){
createXMLHttpRequest();
var url="getTime.php";
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange = callback;
xmlHttp.send(null);

}
function callback(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
document.getElementById("showtime").innerHTML = xmlHttp.responseText;
var result = xmlHttp.responseText;
result = result.split('|');
//                alert(result[0]);
//                alert(result[1]);
document.title = result[1];
document.getElementById("showtime2").innerHTML =  result[2];
setTimeout("start()",1000);
}
}
}
</script>
</html>


getTime.php

<?php
header("cache-control:no-cache,must-revalidate");
header("Content-Type:text/html;charset=utf-8");
$time = "2012-1-20 18:00:00";
$dt_element=explode(" ",$time);
$date_element=explode("-",$dt_element[0]);
$time_element=explode(":",$dt_element[1]);
$date = mktime($time_element[0],$time_element[1],$time_element[2],$date_element[1],$date_element[2],$date_element[0]);
$nowTime = time();
$showtime = date("北京时间Y年m月d日H:i:s",$nowTime);
if($showtime<="北京时间1970年01月01日08:00:00"){
echo "happy new year";
}
echo $showtime."|";
$db=new mysqli();
$db->connect("localhost","root",12345,"test");
$otime=microtime(true);
$db->query("set names 'utf8'");
$sql="select title,content from category where id=1";
$result=$db->query($sql);
$rs=$result->fetch_array();

echo $rs['title']."|";
echo $rs['content'];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ajax 动态 显示 PHP