Flash cs3、Flex與asp、php通信總結(包含資料庫)-Flash actio...
Webjx核心提示: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.d |
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.swfflash與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
",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
",$mysqli->error); $mysqli->close(); exit(); } } } #Returnalistofalltheusers if(!$result=@$mysqli->query("Select*fromusers")){ printf("』flextest』userdatabasequeryselecterror:%s
",$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>
"; } $xml_return.="</users>"; $mysqli->close(); echo$xml_return; ?>
這裡的代碼就是Adobe phpria sdk里的一個實例. 如果想要全部源碼的請留下郵箱,我來發.flex與php的就不說了,直接把php的代碼化成asp代碼就行了,和flash的一道理了.源碼下載:http://www.webjx.com/files/soft/1_071030193505.rar
推薦閱讀:
※對付新鞋磨腳的方法總結
※幼兒教師大班工作總結
※金球獎提名揭曉,我們幫你總結了32部今年值得一看的電影
※句句經典的人性總結,每一句都直指內心,有木有
※研修總結