2012-10-06 4 views
0

OK. J'utilise cette méthode pour obtenir des informations de PHP.Envoyer un tableau de 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"; 

Et plusieurs informations sont envoyées comme var1=1&var2=2

Mais, comment puis-je envoyer un tableau à partir de PHP à AS3? Et comment le gérer avec un like while (tableau) à partir de PHP ou quelque chose.

quelqu'un l'espoir peut me aider ..

Répondre

0

Vous voulez probablement utiliser http_build_query. Exemple:

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

Affichera: key1=value1&key2=value2&key3=value3

+1

C'est la même chose que 'var1 = 1 & var2 = 2', OP voudrait quelque chose comme' arr [] = 1 & arr [] = 2' mais je ne sais pas si AS3 l'interprétera correctement ... – jadkik94

1

Essayez quelque chose comme:

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

Notez que pour générer une telle chaîne avec PHP de http_build_query.

Puis, en AS3, vous split la valeur par une virgule:

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

Sinon, vous pouvez essayer d'utiliser JSON ou XML au lieu de transmettre des données (au lieu de sérialisation dans le format URI).

0

Vous pouvez, et devriez dans le cas de données sensibles (authentification: login, mot de passe) utiliser JSON. Dans Flash Player 11 vous utilisez JSON comme ceci:

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

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

Et en PHP:

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

Dans Flash Player 10 et plus vous aurez besoin de cette bibliothèque: http://code.google.com/p/as3corelib/

Questions connexes