In this tutorial I will show you how to send som data from flash to PHP, encrypting it and sending the answer back again. If you just want the code and no explanation jump to the last page...
I'm just going to get started, so first get create a new flash document, than open the actionspanel. Then lets start coding:
var send:LoadVars = new LoadVars;
var receive:LoadVars = new LoadVars;loadVars.nameOfVariable = valueOfVariable;Here you need to replace the loadVars with the loadVars (in this case send), the nameOfVariable with a variablename (could be nearly anything, but for this case lets use toPHP) and the valueOfVariable with the value of a the variable or the data to send in other words... so lets type
send.toPHP = "test";Good, the next thing to be done is to tell what to do when flash receives the answer, or in other words when the receive loads the content of a site. To do so we use
loadVars.onLoad = function(){
//Here goes the code to execute when it loads
}receive.onLoad = function(){
encrypted = this.toFlash;
trace(encrypted);
}Than we only need to tel flash to load the server script... To do so we use:
loadVars.sendAndLoad("serverScript",anotherLoadVars,"method");send.sendAndLoad("encrypt.php",receive,"POST");Very good tutorial, but you got to be carefull about the folders. I had my animation in root/page/anim and my pages where functioning in root/page and i placed the .php files in the /anim folder cause i thought that it may read from there WRONG! the flash reads from where the page containing the movie is running, in this case root/page , so i moved the file from root/page/anim to root/page and problem solved. Keep in mind that my .swf file was still in /anim.
have a nice day!
Well... This won't be a problem if you add absolute URLs... If your php-file is "/root/admin/php/phpfile.php" and your domain-name is "http://www<dot>somdomain<dot>net" then you could use the path "http://www<dot>somdomain<dot>net/root/admin/php/phpfile.php".
I think you also can use juse "/root/admin/php/phpfile.php" but I'm not shure about that...
Alxandr
Easy to understand, easy to get working. Thanks for the tutorial. After many problems I finally figured out that you need to put an ampersand & in the php code before the first variable when it returns it to the flash.
I was developing this web back end in AS2 to allow for video upload and deployment in units that opened into custom players.
Fairly standard I was about 95% complete just adding some minor delete functionality when the server crashed.
I contacted the host gave out as cpanel chugged back on.
From then on nothing worked, wtf?
I assumed it was some server setting checked all CHMODs .htaccess all the usual culprits, nothing no joy.
I printed the contents of the loadvar receiver and saw this onLoad=%5Btype%20Function%5D hanging out, again wtf?
SO many hours of google trawling later you had thins on the end of an actionscript.org post.
What it was was that for some reason my return php echo of;
echo "returnString=$successString&clientID=$newClient";
was worng and needed to have a & prefixed to it as in your example;
echo "&returnString=$successString&clientID=$newClient";
I have no idea why it worked and stopped but the whole thing was due to be demoed in a few hours and I was going insane.
Thank you,
you are a legend!
please let me know what the name of vars must i write in the var's dynamic text box name..
because my var from PHP to Flash didn't come out..
thanks for your help.