<html>
<head>
<http-equiv="content-type" content="text/html;charset=utf-8"/>
<script type="text/javascript" src="my.js"></script>
<script language="javascript" type="text/javascript">
var myXmlHttpRequest="";
function updateGoldPrice(){
myXmlHttpRequest=getXmlHttpObject();
if(myXmlHttpRequest){
//创建ajax引擎成功
var url="gloadprocess.php";
var data="city[]=dj&city[]=tw&city[]=ld";
myXmlHttpRequest.open("post",url,true);
//必须加的一句话
myXmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
myXmlHttpRequest.onreadystatechange=function chuli(){
//接收数据 json格式
if(myXmlHttpRequest.readyState==4){
if(myXmlHttpRequest.status==200){
//取出并转换成对象数组
var res_objects=eval("("+myXmlHttpRequest.responseText+")");
$('dj').innerText=res_objects[0].price;
$('tw').innerText=res_objects[1].price;
$('ld').innerText=res_objects[2].price;
}
}
}
myXmlHttpRequest.send(data);
}
}
//使用定时器 每隔五秒
window.setInterval("updateGoldPrice()",5000);
</script>
</head>
<body>
<h1>每隔五秒钟更新数据</h1>
<table border=0>
<tr><td colspan="3" style="color:red;padding-left:40px;padding-bottom:20px">金价变动</td></tr>
<tr><td>市场</td><td>最新价格</td><td>涨跌</td></tr>
<tr><td>伦敦</td><td id="ld">788.7</td><td>0</td></tr>
<tr><td>台湾</td><td id="tw">854.0</td><td>0</td></tr>
<tr><td>东京</td><td id="dj">1791.3</td><td>0</td></tr>
</table>
</body>
</html>
<?php
//告诉浏览器编码是utf-8
header("Content-Type: text/html;charset=utf-8");
//告诉浏览器不使用缓存
header("Cache-Control: no-cache");
//接收
$cities=$_POST['city'];//此时cities是数组
//s随机生成三个500-1500之间的数
//$res=[{"price":"400"},{"price":"800"},{"price":"1200"}];
$res='[';
for($i=0;$i<count($cities);$i++){
if($i==count($cities)-1){
$res.='{"cityname":"'.$cities[$i].'","price":"'.rand(500,1500).'"}]';
}else{
$res.='{"cityname":"'.$cities[$i].'","price":"'.rand(500,1500).'"},';
}
}
file_put_contents("d:/mylog.log",$res."\r\n",FILE_APPEND);
echo $res;
?>