金价变动 ajax

<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;
?>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蟹道人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值