就是我现在<a href=”http://www.oeilvert.com” target=”_blank”>http://www.oeilvert.com</a>上面那个,简单写一下,也可以作为Flash和ASP利用XML互相传递数据的例子。
为了方便做教程以及简短起见,安全方面的代码请自行加入。
一个简单的留言本需要:数据库连接,显示,添加,管理员回复和登陆还有删除等几个部分。
1、数据库连接_____conn.asp
<%
Set conn = Server.CreateObject(“ADODB.Connection”)
conn.Open “DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=” & Server.MapPath(“data.mdb”)
%>
不说明了,其他的文件要操作数据库都要调用的。
2、显示
ASP部分:
首先是打开记录集,
<!–#include file=”conn.asp”–>
<%
set rs=server.CreateObject(“adodb.recordset”)
sqlstr=”select * from data order by id desc”
rs.open sqlstr,conn,1,2
%>
为了和Flash沟通,我们需要XML,写在view.asp里面
首先是声明:
<%
response.Write”<?xml version=1.0 encoding=UTF-8?>”
%>
注意使用万国码Unicode。
记录集移动到第一个,注意在数据库里面留一条记录,可以省去判断了。
<%
rs.MoveFirst
Do While Not rs.EOF
%>
使用response.write把xml显示出来,用&rs(“字段名”)&取出记录集的数据:
<%
response.write”<id num=”&rs(“id”)&”>”
response.write”<username><![CDATA[“&rs(“username”)&”]]></username>”
response.write”<qq><![CDATA[“&rs(“qq”)&”]]></qq>”
response.write”<email><![CDATA[“&rs(“email”)&”]]></email>”
response.write”<homepage><![CDATA[“&rs(“homepage”)&”]]></homepage>”
response.write”<content><![CDATA[“&rs(“content”)&”]]></content>”
response.write”<time>”&rs(“time”)&”></time>”
response.write”<reply><![CDATA[“&rs(“reply”)&”]]></reply>”
response.write”</id>”
%>
<%
rs.MoveNext
Loop
%>
<%
response.write”</data>”
%>
<%
rs.close
conn.close
set rs=nothing
set conn=nothing
%>
Flash方面:
var myXML = new XML();
myXML.ignoreWhite = true;//忽略空白
myXML.onLoad = function(success) {
if (success) {
initializingXML();
} else {
gotoAndStop(“error”);
}
};
myXML.load(“http://yourURL/view.asp?r=” + random(65535));//读取xml,加入随机数保证文件是最新的。
stop();
关于如何读取载入XML的元素请参看一些例子,这里不赘述了。
给出输出的XML的结构如下:
<data>
<id num=”序列号”>
<username>姓名</username>
<qq>QQ</qq>
<email>Email</email>
<hp>主页</hp>
<content>留言内容</content>
<time>留言时间</time>
<reply>回复</reply>
</id>
.
.
.
</data>
可以看到myXML.firstChild.childNodes.length就是数据库里面留言的条数。
因为在Flash里面显示每一条记录是一样的,所以我就做了一个ID为message的MovieClip,方便调用。这样写出来的initializingXML函数大致有以下部分(分页部分比较长我先省略):
开始循环:
for (var i = 1; i <= myXML.firstChild.childNodes.length; i++) {
然后是调用,和一些值的设定:
attachMovie(“message”, “message” + i, i);//调用影片剪辑
this[“message” + i]._x = 10;
this[“message” + i]._y = (i – 1) * 115 ;//这个根据message的大小设定。
this[“message” + i].mc_id = i
this[“message” + i].time = myXML.firstChild.childNodes.childNodes[5].firstChild.nodeValue;
this[“message” + i].username = myXML.firstChild.childNodes.childNodes[0].firstChild.nodeValue;
this[“message” + i].content = myXML.firstChild.childNodes.childNodes[4].firstChild.nodeValue;
//判断是否有回复
if (myXML.firstChild.childNodes.childNodes[6].firstChild.nodeValue == “nothing”) {
this[“message” + i].reply = “NO REPLY”;
} else {
this[“message” + i].reply = myXML.firstChild.childNodes.childNodes[6].firstChild.nodeValue;
}
设定QQ,Email,HP按钮的动作:
this[“message” + i].email_btn.onRollOver = function() {
if (myXML.firstChild.childNodes[this._parent.id].childNodes[2].firstChild.nodeValue != “nothing”) {
showTip(“MAIL TO ” + myXML.firstChild.childNodes[this._parent.id].childNodes[2].firstChild.nodeValue);
} else {
this.useHandCursor = false;
}
};
this[“message” + i].email_btn.onRelease = function() {
hideTip();
if (myXML.firstChild.childNodes[this._parent.id].childNodes[3].firstChild.nodeValue != “none”) {
getURL(“mailto:” + myXML.firstChild.childNodes[this._parent.id].childNodes[3].firstChild.nodeValue,”_blank”);
}
};
showtip函数看这里:
<a href=”http://www.oeilvert.com/nov25/showtip.txt” target=”_blank”>http://www.oeilvert.com/nov25/showtip.txt</a>
这个是我在经典上看到的,为青蛙所写,借来用了。
另外初始化函数里面有回复留言和删除留言按钮的初始化代码,在下面讲。
3.添加留言功能的制作
ASP部分—save.asp(可能有漏洞):
向Flash要求变量的值:
<!–#include file=”conn.asp” –>
<%
username=request(“username”)
qq=request(“qq”)
email=request(“email”)
homepage=request(“homepage”)
content=request(“content”)
%>
打开记录并添加新记录:
<%set rs=server.createobject(“adodb.recordset”)
sql=”select * from data where (id is null)”
rs.open sql,conn,1,3
rs.addnew
rs(“username”)=username
rs(“qq”)=qq
rs(“email”)=email
rs(“homepage”)=homepage
rs(“content”)=content
rs.update
rs.close
set rs=nothing
conn.close
set conn=nothing
response.write(“&ok=1”)
%>
返回ok这个变量让Flash接受,表示添加记录完成,下同。
Flash部分:
在舞台上构建各个InputText文本字段或者用TextInput组件也可以。设定好相应的变量名字。
然后制作submit按钮,在上面的on(release)里面添加添加以下AS:
if (_parent.username == “”) {
_parent.error_msg = “请输入你的名字”;
} else if (_parent.content == “”) {
_parent.error_msg = “请输入你的留言”;
} else {
if(_parent.qq=””){
_parent.qq=”nothing”;
}
if(_parent.homepage=””){
_parent.homepage=”nothing”;
}
if(_parent.email=””){
_parent.email=”nothing”;
}
_parent.loadVariables(“http://youURL/save.asp”, “POST”);
_parent.onEnterFrame = function() {
if (_parent.ok == 1) {
delete _parent.onEnterFrame;
_parent.gotoAndStop(“view”);
}
};
}
4.回复留言功能的制作
ASP部分—reply.asp:
<!–#include file=”conn.asp” –>
<%
set rsedit=server.createobject(“adodb.recordset”)
sql=”select * from data where id=” & request(“id”)
rs.open sql,conn,1,3
If request(“ok”) = “1” Then
Else
rs(“reply”)=request(“reply”)
rs.update
rs.close
set rs=nothing
response.write(“&ok=1”)
End If
%>
很简单,不用解释了。
Flash部分:
和添加留言的时候一样,在舞台上添加文本段或组件,设置好相应变量
然后制作submit按钮,在上面的on(release)里面添加添加以下AS:
if (_parent.reply != “”) {
_parent.loadVariables(“http://yourURL/reply.asp”, “POST”);
_parent.onEnterFrame = function() {
if (_parent.ok == 1) {
delete _parent.onEnterFrame;
_parent.gotoAndStop(“view”);
}
};
}
很简单吧?继续~
5.删除留言功能的制作
ASP部分—delete.asp:
<!–#include file=”conn.asp” –>
<%
del=”delete from data where id=” & request(“id”)
conn.Execute del
conn.close
set conn=nothing
response.write(“&ok=1”)
%>
这个,因为简单,存在比较严重的漏洞,8过要做简单的留言本,这样可以实现功能了。
Flash部分:
以下代码加在initializingXML函数里面
this[“message” + i].del_btn.onRollOver = function() {
if (!isLogin) {
this.useHandCursor = false;//用isLogin来判断是否管理员登陆
} else {
showTip(“DELETE”);
}
};
this[“message” + i].del_btn.onRelease = function() {
hideTip();
if (isLogin) {
id = myXML.firstChild.childNodes[this._parent.id].attributes.num;
_root.loadVariables(“http://yourURL/delete.asp?id=” + id, “POST”);
_root.onEnterFrame = function() {
if (ok == 1) {
delete _root.onEnterFrame;
gotoAndPlay(“end”);
}
};
}
};
6.关于管理员登陆
ASP部分—–admin.asp
<!–#include file=”conn.asp”–>
<%
set rs=server.CreateObject(“adodb.recordset”)
sqlstr=”select * from admin where id = 1″
rs.open sqlstr,conn,1,2
%>
<%
If request(“pwd”) = rs(“password”) && request(“adminID”) = rs(“ID”) Then
response.write(“&ok=1”)
Else
response.write(“&ok=2”)
End If
%>
<%
rs.close
conn.close
set rs=nothing
set conn=nothing
%>
Flash部分
做好一个登陆界面,然后设置两个输入文本框,设置变量pwd和adminID
然后再submit上添加onRelease代码:
if (_parent.pwd == “”) {
_parent.error_msg = “PLEASE INPUT PASSWORD”;
} else {
_parent.error_msg = “LOGGING…WAITING FOR RESPONSE…”;
_parent.loadVariables(“http://yourURL/admin.asp”, “POST”);
_parent.onEnterFrame = function() {
if (_parent.ok != 0) {
if (_parent.ok == 1) {
_parent.isLogin = true;
delete _parent.onEnterFrame;
_parent.gotoAndStop(“view”);
} else {
delete _parent.onEnterFrame;
_parent.error_msg = “WRONG PASSWORD”;
}
}
};
}
7.Unicode的设置
为了使各种语言操作系统都能正常观看留言,需要设置Unicode。
步骤如下:
-把所有ASP文件保存为UTF-8格式
-在除conn.asp文件外的所有ASP文件头部添加以下代码:
<%@LANGUAGE=”VBSCRIPT” CODEPAGE=”65001″%>
<%Response.Charset=”UTF-8″%>
-切勿在Flash中使用System.useCodePage=true;
-全文完-