asp+|xml|教程
示範地址
源檔案下載
在下載到本地或者上傳到空間上之前,請到後台修改參數設定裡面的地址,然後進行測試!
如果你的機器或者伺服器不支援FSO,請手動修改URL.XML檔案裡面的地址!
ASP主要部分:
page.asp (傳給flash第n頁的n條紀錄)
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<% show_page = 9 '每頁顯示的紀錄
db = "data/data.mdb" '資料庫存放目錄
'-串連資料庫
set conn=server.createobject("adodb.connection")
conn.open "driver={microsoft access driver (*.mdb)};dbq="&server.mappath(db)
'-----------------------------------------------------------------------
'用途:將UTF-8編碼漢字轉為GB2312碼,相容英文和數字!
function encodestr(str)
dim i
str=trim(str)
str=replace(str,"'","""")
str=replace(str,vbCrLf&vbCrlf,"</p><p>")
encodestr=replace(str,vbCrLf,"<br>")
end function
'用途:將UTF-8編碼漢字轉為GB2312碼,相容英文和數字!
Function uni(Chinese)
For j = 1 to Len (Chinese)
a=Mid(Chinese, j, 1)
uni= uni & "&#x" & Hex(Ascw(a)) & ";"
next
End Function %>
<%
'如果FLASH傳過來變數
if request("action")="showpage" then
'開啟紀錄
sql="select * from gbook order by id desc"
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,1,1
'---------------分頁 開始
if not rs.eof then
'如果有記錄
rs.PageSize = show_page
total=rs.RecordCount '共多少條記錄
maxpage=rs.PageCount '共分幾頁
page=request("page") '當前頁
if Not IsNumeric(page) or page="" then
page=1
else
page=cint(page)
end if
if page<1 then
page=1
elseif page>maxpage then
page=maxpage
end if
rs.AbsolutePage=Page
else
'如果沒記錄
total=0
maxpage=0
page=0
out=""
wujilu="1"
'把wujilu變數傳給flash,讓flash知道沒有記錄然後做出相應的動作
'輸出xml檔案格式
Response.Write "<?xml version='1.0' encoding='utf-8'?>"
Response.Write "<gbook total='"&total&"' maxpage='"&maxpage&"' page='"&page&"' wujilu='"&wujilu&"'></gbook>"
Session.CodePage="936"
end if
'---------------分頁 結束
'---------------開啟 PageSize 條記錄 開始
if not rs.eof then
'如果有記錄
for i=1 to rs.PageSize
page_id=rs("id")
page_name=uni(rs("name"))
if len(rs("title")) > 19 then '截取字元
page_title=left(rs("title"),19)&".."'截取字元
else
page_title=rs("title")
end if
page_title=uni(page_title)
page_date=rs("date")
out=out&"<info page_id='"&page_id&"' page_name='"&page_name&"' page_title='"&page_title&"' page_date='"&page_date&"' />"
rs.movenext
if rs.EOF then
i=i+1
Exit For
end if
next
end if
'---------------開啟 PageSize 條記錄 結束
rs.close
set rs=nothing
conn.close
set conn=nothing'釋放資源
'輸出分頁資訊,xml格式
Response.Write "<?xml version='1.0' encoding='utf-8'?>"
Response.Write "<gbook total='"&total&"' maxpage='"&maxpage&"' page='"&page&"'>"&out&"</gbook>"
Session.CodePage="936"
end if
%>
show.asp(傳給flash單條紀錄的資訊)
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
show_page = 9 '每頁顯示的紀錄
db = "data/data.mdb" '資料庫存放目錄
set conn=server.createobject("adodb.connection")
conn.open "driver={microsoft access driver (*.mdb)};dbq="&server.mappath(db)
'-----------------------------------------------------------------------
'用途:將UTF-8編碼漢字轉為GB2312碼,相容英文和數字!
function encodestr(str)
dim i
str=trim(str)
str=replace(str,"'","""")
str=replace(str,vbCrLf&vbCrlf,"</p><p>")
encodestr=replace(str,vbCrLf,"<br>")
end function
Function uni(Chinese)
For j = 1 to Len (Chinese)
a=Mid(Chinese, j, 1)
uni= uni & "&#x" & Hex(Ascw(a)) & ";"
next
End Function
'------------------------------------------------------------
%>
<%
if request("action")="show" then
'開啟紀錄為flash傳過來的第show_id條資訊
sql="select * from gbook where id="&request("show_id")
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,1,1
'uni參數,就是把gb編碼轉換成utf-8編碼,要不然flash會亂碼
show_name=uni(rs("name"))
show_id=uni(rs("id"))
show_title=uni(rs("title"))
if rs("email")<>"" then
show_email=uni(rs("email"))
end if
if rs("qq")<>"" then
show_qq=uni(rs("qq"))
end if
show_content=uni(rs("content"))
show_date=uni(rs("date"))
out=out&"<info show_name='"&show_name&"' show_id='"&show_id&"' show_title='"&show_title&"' show_email='"&show_email&"' show_qq='"&show_qq&"' show_content='"&show_content&"' show_date='"&show_date&"' />"
rs.close
set rs=nothing
conn.close
set conn=nothing
'輸出xml格式
Response.Write "<?xml version='1.0' encoding='utf-8'?>"
Response.Write "<gbook>"&out&"</gbook>"
end if
%>
add.asp(flash傳給asp增加紀錄)
<%
show_page = 9 '每頁顯示的紀錄
db = "data/data.mdb" '資料庫存放目錄
set conn=server.createobject("adodb.connection")
conn.open "driver={microsoft access driver (*.mdb)};dbq="&server.mappath(db)
'-----------------------------------------------------------------------
'用途:將UTF-8編碼漢字轉為GB2312碼,相容英文和數字!
function encodestr(str)
dim i
str=trim(str)
str=replace(str,"'","""")
str=replace(str,vbCrLf&vbCrlf,"</p><p>")
encodestr=replace(str,vbCrLf,"<br>")
end function
Function uni(Chinese)
For j = 1 to Len (Chinese)
a=Mid(Chinese, j, 1)
uni= uni & "&#x" & Hex(Ascw(a)) & ";"
next
End Function
'------------------------------------------------------------
%>
<%
Session.CodePage="65001"
if encodestr(request("action"))="add" then'如果flash傳過來的變數是add
if encodestr(request("w_name"))="" then'如果w_name等於空
cuowu="n"
response.write"&addok="+cuowu '
elseif encodestr(request("w_title"))="" then '如果標題空
cuowu="t"
response.write"&addok="+cuowu
elseif encodestr(request("w_content"))="" then'如果留言內容空
cuowu="c"
response.write"&addok="+cuowu
end if
if cuowu="" then '如果姓名,標題,留言內容不為空白
sql="select * from gbook "
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,3,3
rs.addnew '增加紀錄
rs("name")=encodestr(request("w_name"))
rs("title")=encodestr(request("w_title"))
if encodestr(request("w_email"))="" then
rs("email")=null
else
rs("email")=encodestr(request("w_email"))
end if
if encodestr(request("w_qq"))="" then
rs("qq")=null
else
rs("qq")=encodestr(request("w_qq"))
end if
rs("content")=encodestr(request("w_content"))
rs("date")=date()
rs.update
rs.close
set rs=nothing
conn.close
set conn=nothing
response.write"&addok=ok" '返回給flash 留言成功
end if
end if
Session.CodePage="936"
%>
FLASH部分:
留言列表
function showPage()//定義函數
{
function titlemenu()
{
gbtitle.attachMovie("title_mc", "title_mc" + i, i);//迴圈增加MC
gbtitle["title_mc" + i]._y = 30 * i;//MC的位置
gbtitle["title_mc" + i].page_id = gb[i].attributes.page_id;//title_mc裡面的動態文本顯示asp傳來的變數
page_id=gb[i].attributes.page_id;
gbtitle["title_mc" + i].page_title = gb[i].attributes.page_title;
gbtitle["title_mc" + i].page_date = gb[i].attributes.page_date;
gbtitle["title_mc" + i].page_name = gb[i].attributes.page_name;
i++;
if (i >= nTotal)//如果紀錄大於每頁顯示的紀錄
{
clearInterval(nInterval);
} // end if
} // End function
total= pageXML.firstChild.attributes.total;
maxpage = pageXML.firstChild.attributes.maxpage;
page = pageXML.firstChild.attributes.page;
tPage = "第 " + page + " / " + maxpage + " 頁 共 " + total + " 條留言";
gb = pageXML.firstChild.childNodes;
nTotal = gb.length;//共幾個節點-共幾條記錄
var nInterval = setInterval(titlemenu, 50);//消極式載入
this.createEmptyMovieClip("gbtitle", 1);//建立空的mc
gbtitle._x = 10;
gbtitle._y = 70;
var i = 0;
} // End function
stop();
pageXML = new XML();//建立對象
pageXML.ignoreWhite = true;//忽略空格
pageXML.load(gbookurl+"page.asp?action=showpage&page="+page + "&temp=" + Math.random() * Number(getTimer()));
pageXML.onLoad = function (success)
{
if (success)//如果讀取成功
{
showPage(1);
//如果載入成功
//xiaoxi = "正在載入資料,請等待...";
//_root.xiaoxikuang.gotoAndPlay("wu");
//_root.jindutiao.gotoAndPlay("wu");
//gotoAndPlay(76);
}
else
{
gotoAndPlay("error");
//如果載入失敗
_root.xiaoxikuang.xiaoxi = "無法串連到資料庫,請檢測網路連接";
} // end if
};// end function
讀取單條紀錄
gbtitle.removeMovieClip();//卸載留言列表
_global.zhen="read";
function Page(index)
{
//把asp傳來的變數傳給動態文本
show_id = "ID:"+read[index].attributes.show_id;
show_name = "留言者:"+read[index].attributes.show_name;
show_title = "標題:"+read[index].attributes.show_title;
show_qq = "QQ:"+read[index].attributes.show_qq;
show_email = "E-mail:"+read[index].attributes.show_email;
show_content = read[index].attributes.show_content;
show_date = "發表時間:"+read[index].attributes.show_date;
} // End function
showXML = new XML();
showXML.ignoreWhite = true;
showXML.load(gbookurl+"show.asp?action=show&show_id=" + go_id + "&temp=" + Math.random() * Number(getTimer()));
showXML.onLoad = function (success)
{
if (success)
{
//如果載入成功
_root.xiaoxikuang.xiaoxi = "正在載入資料,請等待...";
read = showXML.firstChild.childNodes;
Total = read.length;
Page(0);
gotoAndPlay(79);
}
else
{
gotoAndPlay("error");
//如果載入失敗
_root.xiaoxikuang.xiaoxi = "無法串連到資料庫,請檢測網路連接";
} // end if
};// end function
寫紀錄
function sendData()
{
//把動態文本裡的內容傳給send_var
send_var.w_name = _root.w_name;
send_var.w_qq = _root.w_qq;
send_var.w_email = _root.w_email;
send_var.w_title = _root.w_title;
send_var.w_content = _root.w_content;
send_var.sendAndLoad(gbookurl+"add.asp?action=add", load_var, "post");
} // End of the function
send_var = new LoadVars();
load_var = new LoadVars();
fabiao.onRelease = sendData;
load_var.onLoad = function ()
{
if (this.addok=="ok")
{
_root.xiaoxikuang.xiaoxi = "留言成功!";
gotoAndPlay("wok");
}
if (this.addok=="n")
{
_root.xiaoxikuang.xiaoxi = "姓名不可為空!";
gotoAndPlay("werror");
}
if (this.addok=="t")
{
_root.xiaoxikuang.xiaoxi = "標題不可為空!";
gotoAndPlay("werror");
}
if (this.addok=="c")
{
_root.xiaoxikuang.xiaoxi = "留言內容不可為空!";
gotoAndPlay("werror");
}
};//end load_var fun
chongzhi.onRelease = function() //重設按鈕
{
_root.w_name = "";
_root.w_qq = "";
_root.w_email = "";
_root.w_title = "";
_root.w_content = "";
}
stop();
主要用到的代碼就這些,還可以把後台管理功能加在flash裡面,但是因為我正在工作,沒有時間,所以就做了一個簡單的留言本!