Send and load variables with AS3 and PHP

February 21, 2009 by: Christian

Because I use this all the time I decided to put the handling of loading variables with AS3 and PHP in a handy little class. The usage is really simple:

new VarLoader("yourscript.php",{var1:value1, var2:value2})

But before explaining everything in detail, lets look at this example:

flash-as3-php-send-load-variables

The complete AS3 code in varloader.swf:


import net.kaegi.loaders.VarLoader;
var vl:VarLoader;
//
sendBtn.addEventListener(MouseEvent.CLICK,sendBtnHandler);
function sendBtnHandler(e:MouseEvent) {
// Variables sent by POST-Method:
var varObj:Object = {};
varObj.textinput0 = escape(textinput0.text);
varObj.textinput1 = escape(textinput1.text);
// additionally another variable is sent by the GET method:
vl = new VarLoader("http://www.flashcmsframework.ch/wp-content/data/varloader/varloader.php?var_get=I was sent by GET!", varObj);
vl.addEventListener(Event.COMPLETE, onVarsLoaded);
vl.addEventListener(Event.CANCEL, onVarsCancel);
}
function onVarsLoaded(e:Event) {
var msg:String = "Communication with the server was successful.\n\n";
msg += "bulla -> "+e.target.vars.bulla+"\n";
msg += "gogull -> "+unescape(e.target.vars.gogull)+"\n";
msg += "var33 -> "+unescape(e.target.vars.var33)+"\n";
msg += "var_get -> "+unescape(e.target.vars.var_get)+"\n";
msg += "textinput0 -> "+unescape(e.target.vars.textinput0)+"\n";
msg += "textinput1 -> "+unescape(e.target.vars.textinput1)+"\n";
tf_servermsg.textColor = 0x009900;
tf_servermsg.text = msg;
}
function onVarsCancel(e:Event) {
tf_servermsg.textColor = 0x990000;
tf_servermsg.text = e.target.errormsg;
}

…and the code in varloader.php:


// coming from flash (GET)
$var_get = $_GET["var_get"];

// coming from flash (POST)
$textinput0 = $_POST["textinput0"];
$textinput1 = $_POST["textinput1"];

// internal
$bulla = "Blabla. I am just some lonely text.....";
$gogull = "WOW! I was fetched from the server as well! Cool!";
$var33 = "Hey, my name is 'var33'! What's yours?";

// result:
$result = "foo=bar";     // In AS3 the first variable MUST NOT have an '&', otherwise you will get an error message! So just use some dummy vars to make flash happy.
$result .= "&bulla=".rawurlencode($bulla);
$result .= "&gogull=".rawurlencode($gogull);
$result .= "&var33=".rawurlencode($var33);
$result .= "&var_get=".rawurlencode($var_get);
$result .= "&textinput0=".rawurlencode($textinput0);
$result .= "&textinput1=".rawurlencode($textinput1);

echo $result;

?>;

Download source code and example file

I hope someone can use this!

Update 12/28/2009: Some useful links regarding url encoding:
escape()
unescape()
encodeURI()
encodeURIComponent()
decodeURI()
decodeURIComponent()

Cheers,
Christian

Comments

20 Responses to “Send and load variables with AS3 and PHP”
  1. Sadaf says:

    Thanks for an excellent tutorial…

  2. admin says:

    @Sadaf
    You’re welcome! :)

  3. Rikk says:

    Thanks a lot, you just saved the life of one programmer.
    Your tut is full of win. +7 internets and kudos to you, man.

  4. Heidi says:

    Great! Thanks a lot.

  5. YaserKH says:

    vry tnx :)

  6. Rikk says:

    haha, one month later, your handy little class saved me again. Double thanx!!

  7. Ben says:

    Dude, you saved my life!

    Thank you very, very much.

    All the best!

  8. Christian says:

    Wow, I’m a lifesaver! That’s cool… ;-)

  9. Ruben says:

    Amazing post! thank you! :D

  10. Jake says:

    Super! You are quite generous. Thank you for the knowledge share!

  11. Dennis Saunders says:

    Thank you. It did the trick.

  12. Fabio says:

    Thanx, man! Thought in AS3 it wasn’t possible… good job!

  13. Angel says:

    Thank you! i love this easy and clean method !!

  14. shortcut and cool. thanks

  15. Very useful tutorial.Thanks for making it so clear.Bookmarking!It saved my time.

  16. Mann says:

    hi..
    nice work.
    how can load a php Array or object..?

  17. Christian says:

    You can serialize your arrays or objects and convert it in flash again by using “split”. For example like this:
    PHP: $result = “&myarray=”.rawurlencode($v1).”{”.rawurlencode($v2).”{”.rawurlencode($v3);
    AS3: var myarray:Array = e.target.vars.myarray.split(”{”)

    or, if more complex, use JSON.

  18. Nuthman says:

    Interesting, I wrote a AS3 to PHP class that does the same exact thing. check it out at
    http://www.as3blog.org/?p=18 ..This turns out to be a very useful class. Most of my site’s traffic comes to this link!

  19. Mann says:

    i need multidimensional Array loading.
    can i load through flashvars..?

  20. KEN says:

    真的相當實用!非常感謝您!

Trackbacks

Leave a Reply

Comment Spam Protection by WP-SpamFree