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

Page 2 of 5
Creating the PHP file
Don't worry about understanding the PHP right now. It will be explained in a moment. All this script does is accept a number, multiply it by 7 and print that number out to a page. We're going to use it to calculate how old we are in dog years, as one human year supposedly equals seven dog years.4. Open up a word editor and paste in the following code:
<?
$calculation= $_POST['years']*7;
echo $calculation;
?>
Creating the Flash file
5.On the first frame of the main timeline create a small input textbox with an instance name of years .6. Now create a simple button and give it an instance name of myBtn .
Okay, we're cooking with gas now! First I'm going to explain what we're trying to do; The user will be able to enter their age in the input box and press the button. This sends our number to the PHP script, which will multiply it by seven. We're not going to return that number to Flash just yet. Instead we're going to print it out to an HTML page.
The reason we're doing this is because it's an excellent way for bug testing your future Flash/PHP applications. When a PHP/Flash application doesn't work it helps to know where the error lies. This method shows us what PHP is sending back, and if we can see that out PHP output is all good and proper then we know the problem will lie within the Flash part of our app.
The first thing we want to do is set up a LoadVars object (if you haven't used LoadVars before just follow the instructions below and everything should become clear in the next few steps). We'll also set up a variable called path - this has nothing to do with LoadVars. It only serves to make it easier for us to change our site folder structure in the future, if we should want to, with the minimum of hassle.
7. Add the following code to the first frame of the Flash movie:
//=================================
// INIT
//=================================
path = "../nwjv/php/"; //declare path to php files
lvOut = new LoadVars(); //create lv object
Once we have created our LoadVars object we can then create a property for it called year and store our number in there, then send the whole object to the PHP script. Reading about stuff like this before you've done it can make it sound overly complicated, so the best thing is to just do it, and then come back and re-read this paragraph to solidify in your mind what you have just done. Turn to the next page, and we'll add the code.

