Code2Design.com

User login

The Layout

Programming

Graphic Design

Resources

Navigation

C2D Projects

Unsystematic Affiliates

AOM Designs Photoshop Star Tutorials for Geek Learn PHP Free 

Change Language

Who's online

There are currently 1 user and 14 guests online.

Online users

  • davidmosley

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.


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