Ajax+xml ASP Query Code

Source: Internet
Author: User
Table name ly in the database, field name 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" >&nbsp;<input type= "button" value= "Query" onclick= "Javascript:f1 ()" >
</form>
<div id= "disp" >
</div>
</BODY>
</HTML>
<script language= "JavaScript" >
Function F1 ()
{
if (document.form1.search.value== "")
{
Alert (' Please enter the content to be queried ');
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> username:</b>" +user_node+ "<br><b> message:</b>" +text_node+ "<br><b> speaking time: </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 as 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
%>
-->

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.