Labs

It works in my machine!

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

with 16 comments

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

Written by Raúl

November 6th, 2008 at 8:43 pm

Posted in AS3

16 Responses to 'Sending Variables in AS3 A.K.A. sendAndLoad in AS2'

Subscribe to comments with RSS or TrackBack to 'Sending Variables in AS3 A.K.A. sendAndLoad in AS2'.

  1. 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.

    Ronnie

    12 Nov 08 at 18:43

  2. Ahh nevermind I figured it out

    Ronnie

    12 Nov 08 at 18:46

  3. 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.

    raúl

    12 Nov 08 at 20:15

  4. 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
    }

    Ronnie

    13 Nov 08 at 12:23

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

    Wences

    20 Nov 08 at 1:00

  6. 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

    raúl

    20 Nov 08 at 12:20

  7. 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

    haris

    3 Dec 08 at 6:19

  8. 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

    raúl

    3 Dec 08 at 11:58

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

    Mary

    28 Jan 09 at 15:57

  10. you mean the PHP is returning some variables?¿

    raúl

    28 Jan 09 at 16:16

  11. Thank you very much. Very helpful!!!

    Josh Babier

    16 May 09 at 20:51

  12. Im sending strings to a IP control unit (WACI serial Nugget from Aurora multimedia) and it doesnt work in AS3. im sending the exact same thing in AS2 and 3 but it only works for 2. do you think it’s something in the WACI unit? dont get it though? if its sending the exact same message how would it even differentiate the 2

    George

    25 Jul 09 at 11:54

  13. ummm…….. and the WACI unit is returning something??¿¿

    raúl

    27 Jul 09 at 10:04

  14. thanks for posting that small but crucial piece of info regarding the “echo” portion of the php. thought i was losing my mind.

    david

    12 Aug 09 at 7:31

  15. I am doing a project and my head is melted I can not get my login working without an error.Please can you help I have tried hundred of ways but still can not get it working.
    I want to send back the userId from the login table as I use it for a test later in my flash site.

    butSubmit.addEventListener(MouseEvent.CLICK, btnDown);

    function btnDown(event:MouseEvent):void {

    // Assign a variable name for our URLVariables object
    var variables:URLVariables = new URLVariables();
    // Build the varSend variable

    var varSend:URLRequest = new URLRequest(”login.php”);
    varSend.method = URLRequestMethod.POST;
    varSend.data = variables;

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

    variables.userName = userName.text;
    variables.userPassword= userPassword.text;

    variables.sendRequest = “parse”;

    varLoader.load(varSend);

    // When the data comes back from PHP we display it here
    function completeHandler(event:Event):void{
    var loader:URLLoader = URLLoader(event.target);
    var result = loader.data.result;
    //if(result==success){
    //MovieClip(parent).gotoAndStop(”test1″);
    //}
    //var phpVar1 = event.target.data.var1;

    result1_txt.text = result;

    }

    now my php file one of many I HAVE TRIED

    0){

    echo “result=success”;
    }
    else
    {
    echo “result=failure”;
    }
    ?>

    // I was trying this method

    while($row=mysql_fetch_array($result)){

    $userId=urlencode($row['userId']);
    $userName=urlencode($row['userName']);
    echo “userId=$userId”;
    echo “&username=”$userName”;

    sheera

    19 Apr 10 at 5:57

  16. Hi Sheera!
    i don´t know if is too late for this answer hehe but……
    did you fix it??

    raúl

    26 May 10 at 11:02

Leave a Reply