Sending Variables in AS3 A.K.A. sendAndLoad in AS2

in AS2 sending and receiving variables was easy, you could do it even with your eyes closed =D
but in AS3 is another story

remember AS2??

1
2
3
4
var lv:LoadVars = new LoadVars();
lv.username = "ruliman";
lv.password = "123456";
lv.sendAndLoad("somefile.php",lv,"POST");

nice isn’t??

in AS3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var variables:URLVariables = new URLVariables();
variables.username = "ruliman";
variables.password= "123456";
//
trace(variables.toString());
var request:URLRequest = new URLRequest("somefile.php");
request.method = URLRequestMethod.POST;
request.data = variables;
 
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
 
try{
	loader.load(request);
}
catch (error:Error) {
	trace("Unable to load URL");
}

until here you can say “well it isn’t too bad just a bunch of classes, but is better organized =) i love AS3 ”

but……..when you compile Flash tell you this!

Error: Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs.
at Error$/throwError()
at flash.net::URLVariables/decode()
at flash.net::URLVariables$iinit()
at flash.net::URLLoader/flash.net:URLLoader::onComplete()

MUAHAHAAA!!!

the PHP code is simple

1
2
3
4
<?
$user= $_POST["username "];
$pass= $_POST["password"];
?>

so…………where the error is coming from??
i google it and found this answer, is very simple:
my php wasn’t returning something so, put an echo throwing some variable something like:

1
2
3
4
5
6
<?
$user= $_POST["username "];
$pass= $_POST["password"];
 
echo "result=success";
?>

and that’s it everything compiles normal, now i can go back to dinner =D

This entry was posted in AS3. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

11 Comments

  1. Posted 12 November 2008 at 18:43 | Permalink

    So how would I do the as3 equivelant to the ‘andLoad’ part of sendAndLoad?

    I tried loader.addEventListener(Event.COMPLETE, dataIn);

    Need to get data back from the DB once its made the request, but no luck.

  2. Posted 12 November 2008 at 18:46 | Permalink

    Ahh nevermind I figured it out

  3. raúl
    Posted 12 November 2008 at 20:15 | Permalink

    yep, is :

    loader.addEventListener(Event.COMPLETE, onComplete_handler);

    function onComplete_handler(event:Event) {
    var vars:URLVariables = new URLVariables(event.target.data);
    trace(vars);
    }

    you just need to pass all variables in the echo separated by “&”

    cheers.

  4. Posted 13 November 2008 at 12:23 | Permalink

    I did something similar. I made a new loader rather than new variables.

    loader.addEventListener(Event.COMPLETE, onDataIn);

    function onDataIn(e:Event)
    {
    var loader:URLLoader = URLLoader(e.target);
    var result = loader.data.result;
    //result would be what gets echo’d out [ie. result=success
    }

  5. Wences
    Posted 20 November 2008 at 1:00 | Permalink

    I’m pleased to know grupow has a labs blog, this “tip” solves me some questions, thanks. What about using navigateToURL(request)?

  6. raúl
    Posted 20 November 2008 at 12:20 | Permalink

    Hi Wences! thanks for your comment
    as far as i know navigateToURL is like getURL in AS2 but you could send variables via navigateToURL something like:

    var url:String = “http://www.grupow.com”;
    var variables:URLVariables = new URLVariables();
    variables.sessionId = new Date().getTime();
    variables.userLabel = “ruliman”;
    var request:URLRequest = new URLRequest(url);
    request.data = variables;
    try {
    navigateToURL(request);
    }
    catch (e:Error) {
    // handle error here
    }

    but the URLLoader is the fancy way =D

    cheers

  7. haris
    Posted 3 December 2008 at 6:19 | Permalink

    hi all,
    how can i get more than 1 variable. here, name -> result and value->success, but i want to get more than 1 variable.
    actually i want a ComplexObject in response but i dont think it can happen in AS3 so what i m thinking to do is evaluating all the data in the ComplexObject and then form a query string but before i strated to do so I must know do I really able to pass a query string with more than 1 variable or not?

    thanks

  8. raúl
    Posted 3 December 2008 at 11:58 | Permalink

    hi Haris
    umm for a ComplexObject you need to switch to AMF
    you can use AMFPHP is very easy to use
    for receiving more than one variable in the query string you just need to separate all variables by a “&” (you can see URLVariables for more info)

    in the php the echo would be something like :

    echo “result=success&users=user1,user2,user3&importanVar=10&”;

    you can see that users variable the value is “user1,user2,user3″;
    back in AS3 you can split that string and access like an array

  9. Posted 28 January 2009 at 15:57 | Permalink

    And how do I do to get the response in case the PHP file generates some content?

  10. raúl
    Posted 28 January 2009 at 16:16 | Permalink

    you mean the PHP is returning some variables?¿

  11. Josh Babier
    Posted 16 May 2009 at 20:51 | Permalink

    Thank you very much. Very helpful!!!

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*