elva

Ajax方式的留言板

ajax.js

//************************  建立XML对象  **********************

function InitAjax(){
    var http_request = false;
    if (window.XMLHttpRequest){
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType){
            http_request.overrideMimeType('text/html;charset=utf-8');
        }
    }else if (window.ActiveXObject){
        try{
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e){
            try{
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            }catch (e)
            {}
        }
    }
    if (!http_request){
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    return http_request;
}

//*************************   提交留言   ***************************//
function leaveword(fcode,fflag,fpid,fpage){
 var str = '';
// if(fpid==''){alert('参数错误!\n\n请刷新本页后重新提交!');return false;}
 var url = "leaveword.asp?pid=";
 url=url+fpid+'&page='+fpage;//定义网址参数
 if(fflag!='add'){
//  url=url+fpid+'&page='+fpage;//定义网址参数
 }
 else{
  var content=document.getElementById("content").value;
  document.getElementById("content").value='';
  if(content==''){alert('您还没有添写留言内容!');return false;}
  if(content.length>150){alert('留言内容过长!');return false;}
  url+='&content='+content+'&flag='+fflag;
  str = '数据已提交。';
 }
 str+='正在读取留言...';
 document.getElementById('msglist').innerHTML='';
 document.getElementById('msg').innerHTML=str;
 xmlhttp_request=InitAjax();//调用创建XMLHttpRequest的函数
 xmlhttp_request.open('GET', url, true);
 xmlhttp_request.onreadystatechange = function(){
  if (xmlhttp_request.readyState == 4) {
   if (xmlhttp_request.status == 200) {
    var result = xmlhttp_request.responseBody;
    document.getElementById('leaveword').innerHTML = gb2utf8(result);
   }
   else
   {
    var str10 = '<table width="100%" height="30" border="0" cellpadding="0" cellspacing="0"><tr>';
    str10 += '<td><div class="bmbkbc"><table width="100%" border="0" cellspacing="0" cellpadding="0">';
    str10 += '<tr><td height="30" align="center" bgcolor="#FEFAF1"><span class="BMSTYLE4" id="msg">数据读取错误,请刷新本页查看留言内容!</span></td>';
    str10 += '  </tr></table></div></td>      </tr>    </table>';
    document.getElementById('leaveword').innerHTML = str10;
   }
  }
 }
 xmlhttp_request.send(null);
}
//*********************  转换乱码函数  ****************************
function gb2utf8(data){ 
    var glbEncode = []; 
    gb2utf8_data = data; 
    execScript("gb2utf8_data = MidB(gb2utf8_data, 1)", "VBScript"); 
    var t=escape(gb2utf8_data).replace(/%u/g,"").replace(/(.{2})(.{2})/g,"%$2%$1").replace(/%([A-Z].)%(.{2})/g,"@$1$2"); 
    t=t.split("@"); 
    var i=0,j=t.length,k; 
    while(++i<j) { 
        k=t[i].substring(0,4); 
        if(!glbEncode[k]) { 
            gb2utf8_char = eval("0x"+k); 
            execScript("gb2utf8_char = Chr(gb2utf8_char)", "VBScript"); 
            glbEncode[k]=escape(gb2utf8_char).substring(1,6); 
        } 
        t[i]=glbEncode[k]+t[i].substring(4); 
    } 
    gb2utf8_data = gb2utf8_char = null; 
    return unescape(t.join("%")); 

 

Leaveword.asp

 

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<body>
<!-- #include file="conn.asp" -->
<%
Response.buffer=true
Response.Expires = -1
Response.AddHeader "Pragma","no-cache"
Response.AddHeader "cache-ctrol","no-cache"

function GetConn(cityName)
 Set dbConnLocal = Server.CreateObject("ADODB.Connection")
 ConnSQL = ""
 select case Lcase(cityName)
       Case "test"
          ConnSQL = "driver={sql server};database=test;server=.;uid=sa;pwd="    
 end select
 dbConnLocal.open ConnSQL
 set GetConn = dbConnLocal
end function

Set Conn = GetConn("test")
Set Rs = Server.CreateObject("ADODB.RecordSet")
s_Flag = Replace(Request.QueryString("flag"),"'","")
s_Content = Replace(Request.QueryString("Content"),"'","''")
s_ProgramID = Replace(Request.QueryString("PID"),"'","")
s_UserName = Request.Cookies("UserName")
If s_UserName = "" Then
 s_UserName = "游客"
End If
UserIP = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If UserIP = "" Then UserIP = Request.ServerVariables("REMOTE_ADDR")

If UCase(s_Flag) = "ADD" Then
 If s_Content = "" Or Len(s_Content) > 150 Or s_ProgramID = "" Or Not IsNumeric(s_ProgramID) Then
  Response.Write("Error2")
  Response.End()
 End If
 Sql = "Insert Into T_LeaveWord(ProgramID,UserName,IP,Content) Values('"&s_ProgramID&"','"&s_UserName&"','"&UserIP&"','"&s_Content&"')"
 Conn.Execute Sql
End If
Sql = "Select * From T_LeaveWord Where ProgramID='"&s_ProgramID&"' Order By DateTime Desc"
Rs.Open Sql,Conn,1,3
m_Page = Trim(Request("Page"))
If m_Page = "" Or Not IsNumeric(m_Page) Then m_Page = 1
n = 10
i = 1
Rs.PageSize  = n
mRecordCount = Rs.RecordCount
mPageCount  = Rs.PageCount
If Rs.Eof And Rs.Bof Then%>
 <table width="100%" height="30" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td><div class="bmbkbc"><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="30" align="right" bgcolor="#FEFAF1"><span class="BMSTYLE4" id="msg">本页有0条评论0</script></span></td>
  </tr>
</table>
</div></td>
      </tr>
    </table><div id="msglist"></div>
<%Else
 If Cint(m_page) < 1 Or Cint(m_page) > mPageCount then
  m_page = 1
 End If
 Rs.AbsolutePosition = n*(Cint(m_Page)-1)+1%>
 <table width="100%" height="30" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td><div class="bmbkbc"><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="30" align="right" bgcolor="#FEFAF1"><span class="BMSTYLE4" id="msg">总共有<%=mRecordCount%>条评论,第<%=m_page%>页 <%
If m_page <> 1 Then%>
  <A title=首页 href="javascript:leaveword('','','<%=Rs("ProgramID")%>',1);">|&lt;</A>
<%End If
If m_page > 5 Then
 If (m_page Mod 5) = 0 Then
  i_Flag = m_page - 4
 Else
  i_Flag = m_page - (m_page Mod 5) + 1
 End If%>
  <A title=上5页 href="javascript:leaveword('','','<%=Rs("ProgramID")%>',<%=i_Flag - 1%>);">&lt;</A>
<%Else
 i_Flag = 1
End If
For i = i_Flag To mPageCount
 If i = CInt(m_page) Then%>
  <font color=red><%=i%></font>
<% Else%>
  <A href="javascript:leaveword('','','<%=Rs("ProgramID")%>',<%=i%>);"><%=i%></A>
<% End If
 If (i Mod 5) = 0 Then
  Exit For
 End If
Next
If i < mPageCount Then%>
  <A title=下5页 href="javascript:leaveword('','','<%=Rs("ProgramID")%>',<%=i + 1%>);">&gt;</A>
<%
End if
If CInt(m_page) <> CInt(mPageCount) And CInt(mPageCount) > 1 Then%>
  <A title=尾页 href="javascript:leaveword('','','<%=Rs("ProgramID")%>',<%=mPageCount%>);">&gt;|</A>
<%End If%> </span></td>
  </tr>
</table>
</div></td>
      </tr>
    </table><div id="msglist">
<% i = 1
 While Not Rs.Eof And i <= n
  If (i Mod 2) = 0 Then
   s_Color = "#FFFFFF"
  Else
   s_Color = "#F6F6F6"
  End if%>
 <div class="bmbk2">
   <table width="100%" border="1" cellpadding="0" cellspacing="0" bordercolor="#DFDFDF" bgcolor="<%=s_Color%>">
        <tr>
          <td height="80" valign="top"><div class="bmbk3"><div>
      <p align="left"><strong>&nbsp;</strong><span class="BMSTYLE1"><strong>[<%=Rs("UserName")%>]</strong></span> <span class="BMSTYLE4">留言 说:</span></p>
    </div>
    <div class="bmSTYLE7Copy">
              <div align="left">&nbsp;&nbsp;<%=ReplaceImg(Rs("Content"))%></div>
            </div>
   <br>
   <div>
              <div align="right"><span class="bmbkk">发表时间:<%=Rs("DateTime")%> </span></div>
            </div></div></td>
        </tr>
      </table>
 </div>
<%  i = i + 1
  Rs.MoveNext
 Wend
End If
Rs.Close:Set Rs = Nothing
Conn.Close:Set Conn = Nothing
%>
</div>
</body>

 

index.htm

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!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=gb2312">
<title>无标题文档</title>
</head>
<script src="ajax.js"></script>
<body>
<textarea id="content" name="content" cols="60" rows="8" wrap="physical" class="BMSTYLE4"></textarea>
<INPUT style="HEIGHT: 24px" type=button onclick="leaveword('<%=Session("GetCode")%>','add','<%=i_ID%>',1);" value=提交评论><br>
<div id="leaveword"><table width="100%" height="30" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td><div class="bmbkbc"><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="30" align="center" bgcolor="#FEFAF1"><div class="BMSTYLE4" id="msg">正在读取留言...</div></td>
  </tr>
</table>
<div id="msglist"></div>
</div></td>
      </tr>
    </table>
<script language="javascript">leaveword('','','<%=i_ID%>',1);</script>
</div>
</body>
</html>

posted on 2007-05-28 19:49 叶子 阅读(611) 评论(1)  编辑 收藏 引用 所属分类: Ajax


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理