2012-10-06 48 views
0

好的。我正在使用這種方法從PHP獲取信息。從PHP發送數組到AS3?

AS3:

function login_check (e:MouseEvent):void { 

        /* 
        check fields before sending request to php 
        */ 

        if (login_username.text == "" || login_password.text == "") { 

         /* 
         if username or password fields are empty set error messages 
         */ 

         if (login_username.text == "") { 

         login_username.text = "Enter your username"; 

         } 

         if (login_password.text == "") { 

         login_password.text = "Enter your password"; 

         } 

        } else { 

         /* 
         init function to process login 
         */ 

         login_process(); 

        } 

       } 

       /* 
       function to process our login 
       */ 

       function login_process():void { 

        /* 
        variables that we send to the php file 
        */ 

        var phpVars:URLVariables = new URLVariables(); 

        /* 
        we create a URLRequest variable. This gets the php file path. 
        */ 

        var phpFileRequest:URLRequest = new URLRequest("php/controlpanel.php"); 

        /* 
        this allows us to use the post function in php 
        */ 

        phpFileRequest.method = URLRequestMethod.POST; 

        /* 
        attach the php variables to the URLRequest 
        */ 

        phpFileRequest.data = phpVars; 

        /* 
        create a new loader to load and send our urlrequest 
        */ 

        var phpLoader:URLLoader = new URLLoader(); 
        phpLoader.dataFormat = URLLoaderDataFormat.VARIABLES;   
        phpLoader.addEventListener(Event.COMPLETE, login_result); 

        /* 
        now lets create the variables to send to the php file 
        */ 

        phpVars.systemCall = "login_check"; 
        phpVars.login_username = login_username.text; 
        phpVars.login_password = login_password.text; 

        /* 
        this will start the communication between flash and php 
        */ 

        phpLoader.load(phpFileRequest); 

       } 

       /* 
       function to show the result of the login 
       */ 

       function login_result (event:Event):void { 

        /* 

        this autosizes the text field 

        ***** You will need to import flash's text classes. You can do this by: 

        import flash.text.*; 

        */ 

        login_result_text.autoSize = TextFieldAutoSize.LEFT; 

        /* 
        this gets the output and displays it in the result text field 
        */ 

        if (event.target.data.login_result == "1") { 
         login_result_text.text = "Login In..."; 
         this.gotoAndStop("login_loggedin"); 
        }else{ 
         login_result_text.text = "Username and password doesn't match."; 
        } 

       } 

PHP:

print "login_registration_status=1"; 

和多個信息就像var1=1&var2=2

但送,我怎麼能發送陣列從PHP到AS3? 以及如何處理來自PHP或類似的東西(數組)。

希望有人能幫助我..

回答

0

你可能想使用http_build_query。例如:

echo http_build_query(array(
       'key1' => 'value1', 
       'key2' => 'value2', 
       'key3' => 'value3')); 

將輸出: key1=value1&key2=value2&key3=value3

+1

這是同樣的事情'var1 = 1&var2 = 2',OP會想要類似'arr [] = 1&arr [] = 2',但我不知道AS3是否會正確解釋它... – jadkik94

1

試着這麼做:

<?php print 'arr=1,2,3&var1=123&var2=456'; ?> 

需要注意的是用PHP生成的http_build_query這樣的字符串。

然後,在AS3中,你用逗號split值:

var data:Array = arr.split(','); 

否則,你可以嘗試使用JSON代替或XML來傳輸數據(而不是在URI格式序列化)。

0

您可以並且應該在敏感數據(身份驗證:登錄,密碼)的情況下使用JSON。 在Flash Player 11中,您使用JSON這樣的:

var arrayToSend:Array = [ 'abc', 'def', [1, 2, 3] ]; 

var JSONArrayToSend:String = JSON.stringify (arrayToSend); 
var arrayReceived:Array = JSON.parse (JSONArrayReceived); 

而在PHP:

$JSONArrayToSend = json_encode($arrayToSend); 
$arrayReceived = json_decode($JSONArrayReceived); 

在Flash Player 10及以上,你需要這個庫:http://code.google.com/p/as3corelib/