最近初学ajax,xml,现在发布一个ajax+xml的asp查询代码。
数据库中表名ly,字段名y_username,y_message,y_time
index.html
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE> My Documents </TITLE>
</HEAD>
<style>
<!--
body {font-size:12px;}
-->
</style>
<BODY>
<form name="form1" method="post">
<input type="text" name="search"> <input type="button" value="查询" onclick="javascript:f1()">
</form>
<div id="disp">
</div>
</BODY>
</HTML>
<script language="javascript">
function f1()
{
if(document.form1.search.value=="")
{
alert('请输入要查询的内容');
return false;
}
var xmlhttp;
var search;
var s="";
search=document.form1.search.value;
var xmlhttp;
try
{
xmhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp=new XMLHttpRequest();
}
catch (e)
{
}
}
}
xmlhttp.onreadystatechange=function()
{
if(4==xmlhttp.readyState)
{
if(200==xmlhttp.status)
{
var xmldoc=xmlhttp.responseXML;
var message_nodes=xmldoc.getElementsByTagName("message");
var n_messages=message_nodes.length;
for (i=0;i<n_messages;i++)
{
var user_node=message_nodes[i].getElementsByTagName("user")[0].firstChild.data;
var text_node=message_nodes[i].getElementsByTagName("text")[0].firstChild.data;
var time_node=message_nodes[i].getElementsByTagName("time")[0].firstChild.data;
s=s+"<b>用户名:</b>"+user_node+"<br><b>留言:</b>"+text_node+"<br><b>发言时间:</b>"+time_node+"<br><br>";
}
document.getElementById("disp").innerHTML=s;
}
else
{
alert(xmlhttp.status);
}
}
}
var url="search.asp?search="+escape(search)+"&t="+new Date().getTime();
xmlhttp.open("get",url);
xmlhttp.send(null);
}
</script>
search.asp
<!--#include file="adoconn.asp"-->
<%
text=Trim(request("search"))
sql="select y_username,y_message,y_time from ly where y_username like '%"&text&"%' order by y_time desc"
rs.open sql,conn,1,1
If rs.eof Then
str="error"
Else
Response.ContentType="text/xml"
str="<?xml version=""1.0"" encoding=""gb2312""?>"&vbnewline
str=str&"<root>"&vbnewline
i=1
Do While Not rs.eof
str=str&"<message id="""&i&""">"&vbnewline
str=str&" <user>"&rs("y_username")&"</user>"&vbnewline
str=str&" <text>"&rs("y_message")&"</text>"&vbnewline
str=str&" <time>"&rs("y_time")&"</time>"&vbnewline
str=str&"</message>"&vbnewline
i=i+1
rs.movenext
loop
str=str&"</root>"
End If
response.write str
rs.close
set rs = nothing
conn.close
set conn = nothing
%>
本页是由本人开发,转贴请注来源。
评论