做一个网页版聊天室,从HTML界面发送ajax请求到PHP页面,返回数据库中已登录的用户列表,用户列表用jason格式返回,但是到了HTML界面解析不出来。jason的数据格式如下:
["xx001","xx002","xx002","xx002","xx003","xx003","xx003","xx003","xx003","xx003","xx003","xx003","xx004","xx005","xx006","xx007","xx007","xx007","xx008","xx009","xx010","xx010","xx011","xx011","xx011","xx012","xx013","xx013","xx014","xx015","xx016","xx016","xx016"]
["xx001","xx002","xx002","xx002","xx003","xx003","xx003","xx003","xx003","xx003","xx003","xx003","xx004","xx005","xx006","xx007","xx007","xx007","xx008","xx009","xx010","xx010","xx011","xx011","xx011","xx012","xx013","xx013","xx014","xx015","xx016","xx016","xx016"]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="scripts/jquery-1.3.1.js"></script>
<script type="text/javascript">
window.setInterval("processMessage()",1000);
function processMessage(){
$.post("chat.php",{"username":$("#name").text()},function(data){
//alert(data.length);
//var obj = eval("("+data+")");
//window.alert(obj.length);
//var obj = eval("("+data+")");
//alert("ok");
for (i = 0; i < data.length; i++) {
$('#user').append("<li>" +data[i] + "</li>");
}
},"json");
//alert("ok");
//$str = $("#name").text();
//window.alert($str) ;
}</script>
</head>
<body>
<div style="width:800px;display:block;float:left;margin-left:200px;margin-top:10px">
{{$smarty.session.username}} you are welcome!
<center><h1>欢迎光临碧海银沙聊天室</h1></center>
<!--用户列表-->
<div id="left" style="width:200px;height:500px;border:1px solid black;display:block;float:left;">
<ul id="user">
<li>ALL</li>
</ul>
</div>
<!--信息显示框-->
<div id="body" style="width:400px;display:block;float:left;margin-left:20px">
<textarea cols="100" rows="33" >
</textarea>
</div>
<!--信息输入框-->
<div style="width:700px;margin-top:10px;display:block;float:left;">
<span id="name">{{$smarty.session.username}}</span>对 :<input type="text" value="ALL" style="width="88px"/>说:<input type="text" style="width:350px;"> <input type="button" value="发送" id="send"/>
</div>
</div>
</body>
</html>
<?php
require_once 'libs/smarty/Smarty.class.php';
require_once 'libs/SqlHelper.class.php';
require_once 'common.php';class chat extends Common{
function __construct(){
parent::__construct();
}
}
header("content-type: text/xml;charset=utf-8");
$username = $_REQUEST['username'];
$sqlHelper = new SqlHelper();
file_put_contents("d:/mylog.log","ok".$username."\r\n",FILE_APPEND);
$sql = "select username from user where state=0";
$result = $sqlHelper->execute_dql2($sql);
if($result){
$sql = "update user set state=1 where state=0";
$sqlHelper->execute_dml($sql);
}
$jason = json_encode($result);
echo $jason;//file_put_contents("d:/mylog.log","ok".$username."\r\n",FILE_APPEND);
查询语句返回的是资源类型吗?
print_r(json_decode($body,true));
或者我得重新改天数组的结构,可是我把$arr["username"=>"xx001"]赋值给$arr的时候,就$arr就变成了索引数组了,这个有什么函数可以让索引数组赋值的时候是键值和值都保留的么?
这种不是php数组的方式。
php中是$arr = array("username"=>"xx001");