欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Flash cs3、Flex与asp、php通信总结(包含数据库)

程序员文章站 2022-04-08 19:23:30
flash与asp: 先把两个代码贴一下: flash: varloader:URLLoader=newURLLoader(); varurl:URLRequest=newURLRequest("test.asp"); url.method=URLRequestMethod.PO... 08-10-06...
flash与asp:
先把两个代码贴一下:
flash:

varloader:urlloader=newurlloader();

varurl:urlrequest=newurlrequest("test.asp");

url.method=urlrequestmethod.post;

varvalues:urlvariables=newurlvariables();

values.message="helloimflash!";
url.data=values;

loader.dataformat=urlloaderdataformat.variables;
loader.addeventlistener(event.complete,loaded);
functionloaded(e:event){
out1.text=loader.data;
//output:phpmessage=imphpmessage!&flashmessage=helloimflash!
out2.text=loader.data.phpmessage;
//imphpmessage!
out3.text=loader.data.flashmessage;
//helloimflash!
out4.text=loader.data.dbmessage;
}
loader.load(url);

asp:

<%@language="javascript"%>
<%
//接收flash传进来的数据
varflashdata;

flashdata=request("message");
//发给flash的数据
varaspmessage;
aspmessage=’imaspmessage!’;


//数据库的相关操作
//======================================
varconn;
varrs;
varsql;
varstrpath;
strpath="test.mdb";
try{
conn=server.createobject("adodb.connection");
conn.connectionstring="provider=microsoft.jet.oledb.4.0;datasource=" server.mappath(strpath);
conn.open();
}catch(e){
response.write(’<metahttp-equiv="content-type"content="text/html;charset=utf-8"/><pstyle="font-size:12px;font-weight:bold;border:1pxsolid#006;padding:6px;background:#fcc">数据库连接出错,请检查连接字串!</p>’);
response.end
}
rs=server.createobject("adodb.recordset");
sql="select*fromname";
rs.open(sql,conn,1,1);
//======================================
response.write("phpmessage=" aspmessage "&" "flashmessage=" flashdata "&" "dbmessage=" rs("companyname"));

%>

看到flash和asp文件时面变量名就知道相互是怎么传送的呢,对于数据库的一些插入,更新,查询的操作,只要接收flash的数据,直接运行asp相关数据库操作命令就行了.直接复制到iis的路径下即可测试.在线观看地址:http://www.wolf2999.cn/php/test.swf

flash与php:
先把两个代码贴一下:
flash:

varloader:urlloader=newurlloader();

varurl:urlrequest=newurlrequest("test.php");

url.method=urlrequestmethod.post;

varvalues:urlvariables=newurlvariables();

values.message="helloimflash!";
url.data=values;

loader.dataformat=urlloaderdataformat.variables;
loader.addeventlistener(event.complete,loaded);
functionloaded(e:event){
out1.text=loader.data;
//output:phpmessage=imphpmessage!&flashmessage=helloimflash!
out2.text=loader.data.phpmessage;
//imphpmessage!
out3.text=loader.data.flashmessage;
//helloimflash!
out4.text=loader.data.dbmessage;
}
loader.load(url);
php:

<?php
//这里都是有关数据库的操作
//===================================
define(’database_server’,’localhost’);
define(’database_username’,’root’);
define(’database_password’,’root’);
define(’database_name’,’flextest’);
$link=@mysql_connect(database_server,database_username,database_password);
@mysql_select_db(database_name,$link);
$result=@mysql_query("select*fromusers");
$user=@mysql_fetch_array($result,mysql_assoc);
//echo$user[’userid’];
$dbvar=$user[’userid’];
//===================================
//接收flash传进来的数据
$flashdata=$_post[’message’];
//给flash发送的数据
$phpmessage=’imphpmessage!’;
echo"phpmessage=$phpmessage&flashmessage=$flashdata&dbmessage=$dbvar";
?>
代码很简单,不用多解释,其实和asp的没有多大区别的.直接复制到php的路径下即可测试.数据库文件直接在phpmyadmin下面执行flextest.sql里的sql语句就行了.因为公司禁端口,我不能在线操作我空间的sql的数据库,在线演示地址就没有,等有时间在家里加上.

flex与php:
先把两个代码贴一下:
flex:

<?xmlversion="1.0"encoding="utf-8"?>
<mx:applicationxmlns:mx="http://www.adobe.com/2006/mxml"creationcomplete="oninit()"xmlns="*"layout="absolute"backgroundgradientcolors="[#ffffff,#c0c0c0]">
<mx:script>
<![cdata[
publicfunctiononinit():void
{
userrequest.send();
}
]]>
</mx:script>
<mx:httpserviceid="userrequest"url="request.php"useproxy="false"method="post">
<mx:requestxmlns="">
<username>{username.text}</username><emailaddress>{emailaddress.text}</emailaddress>
</mx:request>
</mx:httpservice>
<mx:formx="22"y="10"width="356">
<mx:hbox>
<mx:labeltext="username"/>
<mx:textinputid="username"/>
</mx:hbox>
<mx:hbox>
<mx:labeltext="emailaddress"/>
<mx:textinputid="emailaddress"/>
</mx:hbox>
<mx:buttonlabel="submit"click="userrequest.send()"/>
</mx:form>
<mx:datagridid="dguserrequest"x="22"y="128"dataprovider="{userrequest.lastresult.users.user}">
<mx:columns>
<mx:datagridcolumnheadertext="userid"datafield="userid"/>
<mx:datagridcolumnheadertext="username"datafield="username"/>
</mx:columns>
</mx:datagrid>
<mx:textinputx="22"y="292"id="selectedemailaddress"text="{dguserrequest.selecteditem.emailaddress}"/>
</mx:application>
php:

<?php
/*thankstopetemackieforthecodebelow*/

define(’database_server’,’localhost’);
define(’database_username’,’root’);
define(’database_password’,’root’);
define(’database_name’,’flextest’);

#connecttothedatabase
$mysqli=newmysqli(database_server,database_username,database_password,database_name);

#checkconnection
if(mysqli_connect_errno()){
printf("mysqlconnectfailed:%s\n",mysqli_connect_error());
exit();
}

#quotevariabletomakesafe
functionquote_smart($value){
global$mysqli;
#stripslashes
if(get_magic_quotes_gpc())
$value=stripslashes($value);

#quoteifnotinteger
if(!is_numeric($value))
$value=$mysqli->real_escape_string($value);
return$value;
}

if(!empty($_post)&&$_server[’request_method’]==’post’){
if($_post[’emailaddress’]&&$_post[’username’]){
#addtheuser
$query=sprintf("insertintousersvalues(’’,’%s’,’%s’)",quote_smart($_post[’username’]),quote_smart($_post[’emailaddress’]));
if(!@$mysqli->query($query)){
printf("’flextest’userdatabasequeryinserterror:%s\n",$mysqli->error);
$mysqli->close();
exit();
}
}
}

#returnalistofalltheusers
if(!$result=@$mysqli->query("select*fromusers")){
printf("’flextest’userdatabasequeryselecterror:%s\n",$mysqli->error);
$mysqli->close();
exit();
}

$xml_return="<users>";
while($user=mysqli_fetch_array($result,mysqli_assoc)){
$xml_return.=
"<user><userid>".$user[’userid’]."</userid><username>".$user[’username’]."</username><emailaddress>".$user[’emailaddress’]."</emailaddress></user>\n";
}
$xml_return.="</users>";
$mysqli->close();
echo$xml_return;
?>
这里的代码就是adobe phpria sdk里的一个实例. 如果想要全部源码的请留下邮箱,我来发.flex与php的就不说了,直接把php的代码化成asp代码就行了,和flash的一道理了.

源码下载: