Search code examples
phpactionscript-3flash

how can I migrate to AS3 with code like this "l = new LoadVars ();"


i am a beginner. I'm starting to learn Adobe Flash. I followed the code on Google on AS2:

l = new LoadVars();
l.onLoad = function(ok) {
ttl0=l.ttlslv0; atc0=l.atcslv0;
ttl1=l.ttlslv1; atc1=l.atcslv1;
};
l.sendAndLoad("http:localhost/getdata.php", l, "POST"); 

with php like this:

<?php
include_once("koneksi.php");
$qr = mysqli_query($con,"SELECT title, article from $tabel");
$nr = 0;
while($kolom=mysqli_fetch_row($qr) )
{ 
$ttl=$kolom[0];
$atc=$kolom[1];
echo "&ttlslv$nr=$ttl&atcslv$nr=$atc";
$nr++;
}
echo "&nr=$nr&";
?>
with the results that I can specify "what row" and "what column" will I take. can this be changed to AS3 with the same results? I have difficulty studying AS3 anyone want to give me a solution? thanx...


Solution

  • In AS3 you use the URLLoader class to load text/binary data. To work with URL-encoded strings you need the URLVariables class.

    Something like that (not tested, no error handling either, just a common guideline):

    // URLRequest instance holds various information
    // about what, where and how you send.
    var aRequest:URLRequest = new URLRequest;
    
    // URL address.
    aRequest.url = "http://localhost/getdata.php";
    
    // Request method (POST or GET mostly).
    aRequest.method = URLRequestMethod.POST;
    
    // URLLoader instance performs the requested operations:
    // uploads the request and relevant data, reads the answer,
    // and dispatches all the events about any status changes.
    var aLoader:URLLoader = new URLLoader;
    
    // Tell the URLLoader that the answer is
    // an URL-encoded text of key=value pairs.
    aLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
    
    // This event handler function will be triggered
    // upon successfully completed operation.
    aLoader.addEventListener(Event.COMPLETE, onData);
    
    // Start loading.
    aLoader.load(aRequest);
    
    // The COMPLETE event handler function.
    function onData(e:Event):void
    {
        // Unsubscribe from the event. For the most part it is
        // a GOOD idea NOT to re-use any network-related
        // class instances once they've done their job.
        // Just unsubscribe from all their events,
        // dismantle their data structures, purge
        // any references to them and let the
        // Garbage Collector do his job.
        aLoader.removeEventListener(Event.COMPLETE, onData);
    
        // Retrieve the object that contains key=value pairs.
        var anAnswer:URLVariables = aLoader.data as URLVariables;
    
        // The data you wanted to get.
        trace(anAnswer.ttlslv0);
        trace(anAnswer.atcslv0);
        trace(anAnswer.ttlslv1);
        trace(anAnswer.atcslv1);
    }
    

    UPD: It seems that you are dealing with escaped text there. I composed a simple script that explains how it works:

    import flash.net.URLVariables;
    
    var V:URLVariables = new URLVariables;
    var S:String = "a=1&b=2";
    
    V.decode(S);
    trace(V.a); // 1
    trace(V.b); // 2
    
    S = escape(S);
    trace(S); // a%3D1%26b%3D2
    trace(unescape(S)); // a=1&b=2
    
    V.decode(S); // Error #2101
    

    So, there are two options you can work on:

    1. Figure out why server passes the escaped string and prevent it.
    2. Load the server's answer as a plain text (URLLoaderDataFormat.TEXT instead of VARIABLES) so the URLLoader.data will be just a simple String, then unescape(...) it if necessary and feed to the URLVariables.decode(...).