Send and load variables with AS3 and PHP
February 21, 2009 by: ChristianBecause 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:
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






Thanks for an excellent tutorial…
@Sadaf
You’re welcome!
Thanks a lot, you just saved the life of one programmer.
Your tut is full of win. +7 internets and kudos to you, man.
Great! Thanks a lot.
vry tnx
haha, one month later, your handy little class saved me again. Double thanx!!
Dude, you saved my life!
Thank you very, very much.
All the best!
Wow, I’m a lifesaver! That’s cool…
Amazing post! thank you!
Super! You are quite generous. Thank you for the knowledge share!
Thank you. It did the trick.
Thanx, man! Thought in AS3 it wasn’t possible… good job!
Thank you! i love this easy and clean method !!
shortcut and cool. thanks
Very useful tutorial.Thanks for making it so clear.Bookmarking!It saved my time.
hi..
nice work.
how can load a php Array or object..?
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.
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!
i need multidimensional Array loading.
can i load through flashvars..?
真的相當實用!非常感謝您!