- Home
- Tutorials
- Flash
- Intermediate
- Introduction to Flash and PHP

Page 5 of 5
Neil Webb
Neil Webb is a Flash Platform Developer currently living and working in England. He joined the site when it was just a baby - look at it now! Neil has previously worked for Cambridge University, FIFA and Hutchinson Whampoa among others.
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
Spread The Word
Related Articles
7 Responses to "Introduction to Flash and PHP" 
|
said this on 28 Jun 2007 9:30:09 AM CST
I really appreciate learning the concept rather than trying to decipher a complicated application to meet my needs. Thank you!
|
|
said this on 15 Jul 2007 11:18:04 AM CST
Love the article. I am a PHP programmer working on a Content Management System that I am abstracting in order to provide a content engine not just for HTML/CSS but also for Flash. I have not yet studied AS2/3, but your tutorial was just what I needed to be sure Flash can dynamically access server-side script results and also how it is accomplished. Thanks!
|
|
said this on 29 Sep 2007 7:32:02 AM CST
The careful, line-by-line explanation of code was perfect for me as a designer trying to get his bearings in the world of back-end. Thanks.
|
|
said this on 12 Feb 2008 4:11:50 PM CST
not applicable to actionscript 3. "Access to possibly undefined method LoadVars"
|
|
said this on 06 Oct 2008 7:07:01 AM CST
Hi I tried the tutorial but I get the following Error. I tried the code in the local directory C:\Program Files\Apache Group\Apache2\htdocs\nwjv\
I get the following in Output Text Box ( after I Press the button after I enter the AGE ). $calculation"; ?> |
|
said this on 18 Oct 2008 5:05:35 PM CST
be sure of the files are Saved and Published.
|
|
said this on 18 Oct 2008 5:47:28 PM CST
and you also must be sure that you browse the html by http:// instead of file:///...
|


Author/Admin)