As for the button code, when our button is released we take the value the user has inputted in the Flash input textbox, and add it to our [outgoing] LoadVars object as a new property. To make things easy for ourselves we decide to call the new property years .

lvOut.years = years.text;

The next line is commented out because we don't need it any more and it is the final line that does the main work!

lvOut.sendAndLoad(path + "dogyears.php", lvIn, "POST");

We use the built in sendAndLoad() method of the loadVars object to send data and receive data. Let me explain in more detail:

We use lvOut's sendAndLoad() method instead of its load() method. This allows us to send variables and get them posted back to Flash. To use sendAndLoad we need to send it three arguments; the path of our script, the LoadVars object to which we want our PHP data returned (we created it earlier - lvIn !), and our method of sending, which is "POST". At the beginning of the code you can see that I created an LoadVars object called loadIn , and we get out Flash data sent here because we specified it in the sendAndLoad() method.

We've already set up lvIn's onLoad method, so it knows what to do when data is loaded in to it. If you remember, in the PHP code we set it up to echo a name-value pair, which was basically: returnVal=$calculation;

The name-value pair is returned to Flash - the variable name becomes a property name of the lvIn instance (lvIn.returnVal), and the value gets stored in that property (so lvIn.returnVal will equal $calculation).

Blimey, did you get all that? Okay, let's have a look at the finished file first, and then I will recap over the whole thing so you can get it clear in your mind. Generate the files and upload them or whatever you need to do. Then open up dogYears.html ... (REMEMBER TO DO THIS VIA LOCALHOST IF TESTING LOCALLY - ie the address in your browser bar should be something like http://localhost/nwjv/001_dogYears.html)

... enter a number into the input box and press the button. The value of the calculation should now appear in the output textbox - you can see that the data has gone to PHP, been manipulated and then sent back and displayed in Flash!

Review of what we did

Basically, flash sends PHP a variable called "years". PHP then manipulates that variable, and echos a name-value pair (ie a variable and its value) to Flash. PHP calls the variable "returnVal". Flash then accesses returnVal from the LoadVars object it was returned to, and displays the value in a textbox.

Obviously PHP wasn't really needed for this functionality. We could have done it all in Flash with much less hassle, but this tutorial demonstrates how to get data from Flash to PHP and back again.

One last thing to note: LoadVars also has an onData method, but using both onLoad and onData at the same time causes problems and may cause the movie to cease functioning (using onData prevents onLoad from ever being called and is a know Flash bug). There are ways around this, such as explicitly calling onLoad from inside onData, but in most cases using one of the two will suffice. For now it is just something to be aware of.

Well, once again we've reached that point where I say "...and that's it for this tutorial" like it's some dodgy catch phrase. I hope it's proved a useful intorduction to PHP and Flash.

If you have any suggestions or comments about this or any of my tutorials you can email me and I will do my best to answer them. Please note that due to the volume of Flash-related emails I get, I now prioritize emails related directly to the tutorials themselves. You may find answers to your questions already posted on Flash forums such as actionscript.org and were-here.com

This, and other tutorials can be found on nwebb.co.uk