flex php 交互 登录注册简单示例

flex php 交互 登录注册简单示例
 
 

 login.mxml

 

 

 

<?xml version="1.0" encoding="utf-8"?>  
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">  
<mx:Script>  
    <![CDATA[  
        import mx.collections.ArrayCollection;  
        import mx.rpc.events.ResultEvent;  
        import mx.controls.Alert;  
        [Bindable]  
        private var result1:ArrayCollection;  
        private var login_result:String;  
        [Bindable]  
        private var sendChoice:String;  
        private function goLogin():void{  
            if(username.text=="" || userpwd.text=="")  
               Alert.show("没有填写用户名或密码");  
            else{  
            sendChoice="login";  
            login.send();  
            }  
        }  
        private function goRegis():void{  
            if(username.text=="" || userpwd.text=="")  
              Alert.show("没有填写用户名或密码");  
            else{  
            sendChoice="regis";  
            login.send();  
            }         
        }  
          
        private function resultHandler(event:ResultEvent):void{  
         login_result=event.result.html.body.users.a.toString();  
         if(login_result=="ok"){  
          Alert.show("欢迎,登录成功");  
         }        
         if(login_result=="regisok"){  
          Alert.show("注册成功,请从新登陆");  
         }  
         if(login_result=="error"){  
          Alert.show("错误");  
         }  
        }  
    ]]>  
</mx:Script>  
    <mx:HTTPService id="login" method="POST" showBusyCursor="true" url="http://localhost/login.php"   
        result="resultHandler(event)">  
    <mx:request xmlns="">  
        <mx:username>  
            {username.text}  
        </mx:username>  
        <mx:userpwd>  
            {userpwd.text}  
        </mx:userpwd>  
        <mx:sendchoice>  
            {sendChoice}  
        </mx:sendchoice>  
    </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" restrict="0-9,a-z" maxChars="8"/>  
        <mx:TextInput x="92" y="95" id="userpwd" fontSize="12" displayAsPassword="true" maxChars="8" restrict="0-9,a-z"/>  
        <mx:Button x="78" 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" click="goRegis()"/>  
        <mx:Label x="10" y="10" text="测试用 用户名 user 密码 1234" fontSize="12" width="243"/>  
    </mx:Panel>  
</mx:Application> 

 

 

login.php

<!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]) && isset($_POST[sendchoice])){  
  $uname=$_POST[username];  
  $upwd=$_POST[userpwd];  
  $choice=$_POST[sendchoice];
  if($choice == "login"){
     $return='<users>';  
     $return.='<a>ok</a>';  
     $return.='</users>'; 
  }
  if($choice == "regis"){
     $return='<users>';  
     $return.='<a>regisok</a>';  
     $return.='</users>';  
  }
} else{  
 $return='<users>';  
 $return.='<a>error</a>';  
 $return.='</users>';  
}    
  echo $return;  
?>  
</body>  
</html>