【php开源代码栏目提醒】:网学会员为需要php开源代码的朋友们搜集整理了18_5.php相关资料,希望对各位网友有所帮助!
<?php
if(isset($_GET["action"]) and $_GET["action"]=="getJSON"){
//发送头文件
header('Content-Type:text/html;charset=GB2312');
//连接数据库
mysql_connect("localhost","root","pass
word");
mysql_select_db("learn");
//设置字符集
mysql_query("set names gb2312");
//转换外部变量,防止SQL注入
$intnumber = intval($_GET["number"]);
if($intnumber>=1){
//取出数据
$result = my
sql_query("select * from user LIMIT 0,{$intnumber}");
//定义用于存储JSON数据变量
$jsonArray = array();
while($user = mysql_fetch_array($result)){
//取出数组单元,对中文字符进行URL编码
$user[0] = urlencode($user[0]);
$user[1] = urlencode($user[1]);
$user[2] = urlencode($user[2]);
$user[3] = urlencode($user[3]);
$user[4] = urlencode($user[4]);
$user[5] = urlencode($user[5]);
$user[6] = urlencode($user[6]);
//把数组转化为JSON数据
$jsonArray[] = json_encode($user);
}
//构建JSON数据
$json = "{user:[".implode(",",$jsonArray)."]}";
//输出JSON数据
print $json;
}else{
print "非法数据格式!";
}
exit();
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD
HTML 4.01 Frameset//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>
PHP AJAX XML</title>
<?php
include_once("18_1.php");
?>
<script type="text/javascript" language="javascript 1.2">
function getContent(){
//定义要提交脚本的地址
var url = "18_5.php";
var number = document.getElementById("number").value;
var action = "action=getJSON&number="+number;
//指定回调函数
xmlHttp.onreadystatechange = showXML;
//使用GET方法提交数据
xmlHttp.open("GET",url+"?"+action,true);
//发送请求
xmlHttp.send(null);
}
//回调函数
function showXML(){
//提交请求后的状态
if (
xmlHttp.readyState == 4) {
//HTTP请求的状态
if (xmlHttp.status == 200) {
//使用eval()引用JSON数据
var jsonObj = eval('('+xmlHttp.responseText+')');
for(var i=0;i<jsonObj.user.length;i++){
var row = Array(7);
row[0]=urlDecoding(jsonObj.user[i][6]);
row[1]=urlDecoding(jsonObj.user[i][0]);
row[2]=urlDecoding(jsonObj.user[i][1]);
row[3]=urlDecoding(jsonObj.user[i][2]);
row[4]=urlDecoding(jsonObj.user[i][3]);
row[5]=urlDecoding(jsonObj.user[i][4]);
row[6]=urlDecoding(jsonObj.user[i][5]);
addrow("userList",row);
}
} else {
alert('HTTP请求错误');
}
}
}
//把数组中的内容添加到指点表的函数
function addrow(tablename, array){
var table = document.getElementById(tablename);
var lastRow = table.rows.length;
var row = table.insertRow(lastRow);
for (r = 0; r < array.length; r++) {
var cell = row.insertCell(r);
cell.innerHTML = array[r];
}
}
</script>
<SCRIPT LANGUAGE="VBScript">
<!--
//使用
VBScript解析URL编码
Function urlDecoding(vstrin)
Dim i,strreturn,strSpecial,thischr,intasc
strSpecial = "!""#$%&'()*+,-./:;<=>?@[\]^_`{|}~"
strreturn = ""
For i = 1 to len(vstrin)
thischr = mid(vstrin,i,1)
If thischr="%" Then
intasc=eval("&h"+mid(vstrin,i+1,2))
If instr(strSpecial,chr(intasc))>0 Then
strreturn= strreturn & chr(intasc)
i=i+2
Else
intasc=eval("&h"+mid(vstrin,i+1,2)+mid(vstrin,i+4,2))
strreturn= strreturn & chr(intasc)
i=i+5
End If
Else
If thischr="+" Then
strreturn= strreturn & " "
Else
strreturn= strreturn & thischr
End If
End If
Next
urlDecoding = strreturn
End Function
//-->
</script>
</head>
<body>
<form>
取出的记录数:<input type="text" value="" id="number"><input type="button" value="取得数据库记录" onclick="getContent()" />
</form>
<p><table border="1" id="userList"><tr><th>ID</th><th></th><th>性别</th><th>年龄</th><th>生日</th><th>省份</th><th>备注</th></tr></table></p>
</body>
</html>