Code2Design.com

User login

The Layout

Programming

Graphic Design

Resources

Navigation

C2D Projects

Unsystematic Affiliates

Open Web Design Capital Tutorials T-Tutorials Project 62 

Change Language

Who's online

There are currently 0 users and 1 guest online.

Sending variables from flash to PHP and back.

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

Part 1. Flash

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;

This simply creates two loadvars variables called send and receive. We use loadvars to send data from flash to PHP (or any other server-script) and geting the answer back.
than we need to give the send loadvars som data to send. To do this we use
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
}

Here too we need to change out the loadVars with a loadvars, but however, we must not replace the function() whit somthing... This is just the syntax of actionscript...
So what we want to do is to show the result of what the PHP script has sent... In the PHP part we're going to use toFlash as variablename when we send it back to flash, and to get that in flash we use:
receive.onLoad = function(){
encrypted = this.toFlash;
trace(encrypted);
}

(The trace only outputs somthing, that means that the answer is putputed in the output-panel)...

Than we only need to tel flash to load the server script... To do so we use:

loadVars.sendAndLoad("serverScript",anotherLoadVars,"method");

replase the loadVars with the sender and the anotherLoadVars with the receiver. The serverScript is simply a referance to the server script file... The method is eather get or post... I normaly prefer post (actually I've never used get, I don't even know if it works :p )... That would mean that ouer code would be like this:
send.sendAndLoad("encrypt.php",receive,"POST");


Submitted by Alxandr on January 18, 2007 - 7:23pm. |
printer friendly version

Watch the folder you're in!

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

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


Finally a sendAndLoad tutorial that is short, simple and works.

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.


Thank you this article helped me fix a bug that drove me mad

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!


i have lil' problem

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.


Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <br /> <h3>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use BBCode tags in the text, URLs will be automatically converted to links
More information about formatting options



Like what you see?

Why not add more? C2D is looking for other Christian Web Masters who would like to help write articles for this site. If you have expericance in FLASH, CSS/HTML, PHP/MySQL, PhotoShop/GIMP, Blender, Javascript, or just General Design - our users would love to hear what you have to say. Contact Us

delicious   digg   reddit   magnoliacom   newsvine   furl   google   yahoo   technorati