FLEX PHP 交互 简单登录界面(1)源代码

 

简单的不能再简单的flex 与 php 交互的 登录界面 (没有使用mysql)

主要是解决 httpService 的一些问题 代码在附件中 (本例用flex builder 3 和 Deamweaver cs3 完成 下载代码 配置你的服务器 flex 工程直接导入)

要完成一个表单的提交和返回要通过以下几个环节

Flex 端全部代码代码

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">  
  3. <mx:Script>  
  4.     <![CDATA[  
  5.         import mx.rpc.events.ResultEvent;  
  6.         import mx.controls.Alert;  
  7.         private function goLogin():void{  
  8.             login.send();  
  9.         }  
  10.         private function resultHandler(event:ResultEvent):void{  
  11.             Alert.show(event.result.html.body.users.a.toString());  
  12.         }  
  13.       ]]>  
  14. </mx:Script>  
  15.     <mx:HTTPService id="login" method="POST" showBusyCursor="true" url="http://localhost/flexlogin.php"
  16.         result="resultHandler(event)">  
  17.     <mx:request xmlns="">  
  18.         <mx:username>  
  19.             {username.text}  
  20.         </mx:username>  
  21.         <mx:userpwd>  
  22.             {userpwd.text}  
  23.         </mx:userpwd>  
  24.     </mx:request>  
  25.     </mx:HTTPService>  
  26.     <mx:Panel width="310" height="265" layout="absolute" title="登录" fontSize="12" fontWeight="normal">  
  27.         <mx:TextInput x="93" y="51" id="username" fontSize="12"/>  
  28.         <mx:TextInput x="92" y="95" id="userpwd" fontSize="12" displayAsPassword="true"/>  
  29.         <mx:Button x="73" y="154" label="登录" id="btn1" click="goLogin()" fontWeight="normal" fontSize="12"/>  
  30.         <mx:Label x="32" y="53" text="用户名:" fontSize="12"/>  
  31.         <mx:Label x="43" y="97" text="密码:" fontSize="12"/>  
  32.         <mx:Button x="154" y="154" label="注册" fontSize="12" fontWeight="normal" id="btn2"/>  
  33.         <mx:Label x="10" y="10" text="测试用 用户名 User 密码 123456" fontSize="12" width="243"/>  
  34.     </mx:Panel>  
  35. </mx:Application> 
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
	<![CDATA[
		import mx.rpc.events.ResultEvent;
		import mx.controls.Alert;
		private function goLogin():void{
			login.send();
		}
		private function resultHandler(event:ResultEvent):void{
			Alert.show(event.result.html.body.users.a.toString());
		}    ]]>
</mx:Script>
    <mx:HTTPService id="login" method="POST" showBusyCursor="true" url="http://localhost/flexlogin.php" 
    	result="resultHandler(event)">
    <mx:request xmlns="">
    	<mx:username>
    		{username.text}
    	</mx:username>
    	<mx:userpwd>
    		{userpwd.text}
    	</mx:userpwd>
    </mx:request>
    </mx:HTTPService>
	<mx:Panel width="310" height="265" layout="absolute" title="登录" fontSize="12" fontWeight="normal">
		<mx:TextInput x="93" y="51" id="username" fontSize="12"/>
		<mx:TextInput x="92" y="95" id="userpwd" fontSize="12" displayAsPassword="true"/>
		<mx:Button x="73" y="154" label="登录" id="btn1" click="goLogin()" fontWeight="normal" fontSize="12"/>
		<mx:Label x="32" y="53" text="用户名:" fontSize="12"/>
		<mx:Label x="43" y="97" text="密码:" fontSize="12"/>
		<mx:Button x="154" y="154" label="注册" fontSize="12" fontWeight="normal" id="btn2"/>
		<mx:Label x="10" y="10" text="测试用 用户名 User 密码 123456" fontSize="12" width="243"/>
	</mx:Panel>
</mx:Application>

(flex 全部代码)

  (1)httpservice  标签参数表

  <mx:HTTPService
Properties
concurrency="multiple|single|last"
contentType="application/x-www-form-urlencoded|application/xml"
destination="DefaultHTTP"
id="No default."
method="GET|POST|HEAD|OPTIONS|PUT|TRACE|DELETE"
resultFormat="object|array|xml|e4x|flashvars|text"
showBusyCursor="false|true"
makeObjectsBindable="false|true"
url="No default."
useProxy="false|true"
xmlEncode="No default."
xmlDecode="No default."
Events
fault="No default."
result="No default."
/>
几个需要注意的地方 url 为你要请求的网址 result 是返回结果处理的函数,函数要给一个参数为event,事件类型为ResultEvent

Flex代码

  1. <mx:HTTPService id="login" method="POST" showBusyCursor="true" url="http://localhost/flexlogin.php"
  2.     result="resultHandler(event)">  
  3.    <mx:request xmlns="">  
  4.     <mx:username>  
  5.         {username.text}  
  6.     </mx:username>  
  7.     <mx:userpwd>  
  8.         {userpwd.text}  
  9.     </mx:userpwd>  
  10.    </mx:request>  
  11.    </mx:HTTPService> 
 <mx:HTTPService id="login" method="POST" showBusyCursor="true" url="http://localhost/flexlogin.php" 
    	result="resultHandler(event)">
    <mx:request xmlns="">
    	<mx:username>
    		{username.text}
    	</mx:username>
    	<mx:userpwd>
    		{userpwd.text}
    	</mx:userpwd>
    </mx:request>
    </mx:HTTPService>

httpService 标签

request 标签为你要请求发送的表单,再往里面 <mx:username> 为自己定义,注意:你面的名称为向php方接收的参数名称。

这段代码发送了两个参数 一个是 username 一个是 userpwd

Flex代码

  1. private function goLogin():void{  
  2.             login.send();  
  3.         } 
private function goLogin():void{
			login.send();
		}

然后点击登录 ,由httpservice 实例的.send()发送请求 (客户端的代码先告一段落)

下面是php端的全部代码

Php代码

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5. <title>flex login</title>  
  6. </head>  
  7. <body>  
  8. <?php       
  9. $return="";  
  10. if(isset($_POST[username]) && isset($_POST[userpwd])){  
  11. if ("User"==$_POST[username] && "123456"==$_POST[userpwd])  
  12. $return="ok";  
  13. else
  14. $return="error";  
  15. }  
  16. $xml_return = '<users>';  
  17. $xml_return.= '<a>'.$return.'</a>';  
  18. $xml_return.= '</users>';  
  19. echo $xml_return;  
  20. ?>  
  21. </body>  
  22. </html> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>flex login</title>
</head>

<body>
<?php     
$return="";
if(isset($_POST[username]) && isset($_POST[userpwd])){
 if ("User"==$_POST[username] && "123456"==$_POST[userpwd])
    $return="ok";
 else
    $return="error";
}
$xml_return = '<users>';
$xml_return.= '<a>'.$return.'</a>';
$xml_return.= '</users>';
echo $xml_return;

?>
</body>
</html>

(注意:这是在Dreamweaver cs3 中写的代码)根据你写代码的不同返回的结果不同

  需要说一下的是返回的是什么?

    返回的是一个对象 为什么 event.result.html.body.users.a 呢?

    首先可以确定的是 event.result这部分后面就要看情况了。

    我们可以用event.message.body.toString() 看一下 (结果如下)

Java代码

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5. <title>flex login</title>  
  6. </head>  
  7. <body>  
  8. <users><a><STRONG>ok</STRONG></a></users></body>  
  9. </html> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>flex login</title>

</head>



<body>

<users><a>ok</a></users></body>

</html>

  所以要根据返回的结果的不同 来分析event 到底是一个什么养的对象 否则放回的结果永远是[object Object]

  所以本例的处理函数为

Java代码

  1. private function resultHandler(event:ResultEvent):void{  
  2.             Alert.show(event.result.html.body.users.a.toString());  
  3.         } 
private function resultHandler(event:ResultEvent):void{
			Alert.show(event.result.html.body.users.a.toString());
		}

  论坛上还有一个例子是只有php代码的<?php  ?> 不知道event.result 后面怎么写的时候 ,可以尝试打印一下 event.message.body.toString() ;

posted @ 2010-01-14 08:58  桀歌  阅读(2331)  评论(1)    收藏  举报