52,797
社区成员
发帖
与我相关
我的任务
分享
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
Response.Charset="gb2312"
%>
<%'if session("username")="" then
'response.Redirect("transition.asp")
'else%>
<%'end if%>
<%
'处理ajax请求,每一秒中更新显示数据库聊天记录到显示面板!(注显示面板也在此asp文件中)
if request.querystring("fordisplay")<>"" then
dim conn,cnnstr,sqlforquery,rs,numberfordisplayeditem
numberfordisplayeditem=30
set conn=server.CreateObject("adodb.connection")
cnnstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("chattingroom.mdb")
conn.open cnnstr
set rs=server.CreateObject("adodb.recordset")
sqlforquery="SELECT TOP "&numberfordisplayeditem&" * FROM chatting ORDER BY d desc"
rs.open sqlforquery,conn,1,1
if not rs.eof then
rs.movelast
do while not rs.bof
response.Write rs("d")&" 聊友 "&rs("username")&"说: "&replace(rs("content"),vbcrlf,"<br>")&"<br>"
rs.moveprevious
loop
end if
rs.close
set rs=nothing
conn.close
set conn=nothing
response.write "<a name=""end""></a>"
response.end
%>
<%
'处理ajax请求,更新数据库新聊天记录!
elseif request("forsend")<>"" then
set conn=server.createobject("adodb.connection")
cnnstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("chattingroom.mdb")
set rs=server.createobject("adodb.recordset")
'以下一行需要改善,还不完善!请注意! session这边,还有头部需要用判断语句来判断是否是未登录用户直接非法登录了!
mysql="INSERT INTO CHATTING (USERNAME , CONTENT ) VALUES ('"&session("username")&"','"&request("forsend")&"')"
'response.write("<script type=""text/javascript"">alert(""发送成功!"");</script>")
conn.open cnnstr
rs.open mysql,conn,3,3
response.write("我成功了")
set rs=nothing
conn.close
set conn=nothing
end if
%>
<!-- 如下是正常也就是第一次访问该asp产生的html代码,就第一次进入该页面要用到-->
<!-- 它也是显示框架-->
<!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>
<style type="text/css">
div
{
overflow:auto;
width:1000px;
height:500px;
}
form
{
position:relative;
}
input
{
position:absolute;
top:200px;
left:600px
}
textarea
{
position:absolute;
top:150px;
left:170px;
width:400px;
height:100px;
}
</style>
<body onload="location.href='#end';">
<div id="panel"></div >
<form>
<textarea name="content" cols="11" rows="6"></textarea>
<input type="button" name="send" value="发送" />
<input style="left:650px;" type="button" name="exit" value="注销" />
<span id="prompt" style="position:absolute;left:600px;top:240;"></span>
</form>
</body>
<script type="text/javascript">
var xmlobj=null;
var xmlobj2=null;
window.onload=function(){
last();
//按钮部分
document.getElementsByName("send")[0].onclick=function(){
var r = new RegExp("\\s*\\S+\\s*","g");
if(document.getElementsByName("content")[0].value && r.test(document.getElementsByName("content")[0].value))
{
xmlobj2 =operation("display.asp?forsend="+document.getElementsByName("content")[0].value,"get",forsend);
xmlobj2.setRequestHeader("If-Modified-Since","0");
xmlobj2.send(null);
}
else
{
alert("不能留空或者输入空白字符,请重新输入!");
document.getElementsByName("content")[0].value="";
document.getElementsByName('content')[0].focus();
}
}
}
function forchange()
{
if (this.readyState == 4 )
{
var panel = document.getElementById("panel");
//alert(xmlobj.responseText);
panel.innerHTML=this.responseText;
location.href="#end";
setTimeout("last();",500);
}
}
function forsend()
{
if(this.readyState ==1 ||this.readyState == 2 || this.readyState == 3)
{
document.getElementById("prompt").innerHTML="正在发送...";
}
else if (this.readyState == 4)
{
document.getElementById("prompt").innerHTML="";
}
}
function operation(url,way,method)
{
var temp;
if(window.XMLHttpRequest)
{
temp=new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
try
{
temp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try{
temp=new ActiveXObject("Microsoft.XMLHTTP")
}
catch(e)
{
alert("你的浏览器不支持Ajax!");
}
}
}
if(temp)
{
temp.onreadystatechange=method;
temp.open(way,url,true);
return temp;
}
}
function last()
{
xmlobj=operation("display.asp?fordisplay=yes","get",forchange);
xmlobj.setRequestHeader("If-Modified-Since","0");
xmlobj.send(null);
}
</script>
</html>
var xmlHttp;
function createXmlRequest()
{
if(window.ActiveXObject) {
var xmlHttps=["MSXML2.XMLHttp.5.0","MSXML2.XMLHttp4.0", "MSXML2.XMLHttp3.0","MSXML2.XMLHttp","Microsoft.XMLHTTP"]; try {
for(var i=0;i<xmlHttps.length;i++) {
var xmlHttp=new ActiveXObject(xmlHttps[i]);
return xmlHttp;
}
} catch (error) { }
} else {
var xmlHttp=new XMLHttpRequest();
return xmlHttp;
}
throw new Error("无法创建xmlhttprequest对象");
}
function showMes(){
xmlHttp=createXmlRequest();
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.open("GET","Handler.ashx",true); xmlHttp.send(null);
}
function handleStateChange(){
if(xmlHttp.readyState==4){
document.getElementById("divMes").innerHTML=xmlHttp.responseText;
}
}
</script>
<body onload="setTimeOut('showMes()',1000);">
SqlConnection ...
SqlCommand cmd=...
SqlDataReader dr=cmd.ExecuteReader();
String returnValue="<table>";
if(dr.HasRow)
{
While(dr.Read())
{
returnValue+="<tr><td>"+dr["mes"].ToString()+"</td></tr>";
}
}
returnValue+="<table>";
context.Response.write(returnValue);context.Response.Flush();