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

53 Responses to “Send and load variables with AS3 and PHP”
  1. myth says:

    i really don’t know what the problem but i will use navigateToURL and the GET function to send parameters..

  2. Christian says:

    If you like, you can send me a download link to your project, so I can have a look at the whole picture.

    Cheers,
    Christian

Leave a Reply