金价变动 ajax

来源:互联网 发布:电脑网络重置后怎么办 编辑:程序博客网 时间:2024/04/29 00:51
<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-8header("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;?>


0 0
原创粉丝点击