ajax+xml的asp查詢代碼

來源:互聯網
上載者:User
資料庫中表名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">&nbsp;<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
%>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.