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

Page 3 of 5
8. Add the following code to the same frame:
//=================================
// BTN CODE
//=================================
myBtn.onRelease = function(){
//assign user-input value to lv property called years
lvOut.years = years.text;
//send to a blank window
lvOut.send(path + "dogyears.php","_blank");
};
In the above code we specify an onRelease event for our button. When the button is released we create a new property called years , and we assign it the value of our user input box.
Next we use the send method of our LoadVars object to send our object to whatever script we specify. The two arguments supplied are URL and TARGET. For the URL we use our path variable (which is set to equal "../nwjv/php/"), plus the name of our script (dogyears.php); and for the target we specify "_blank" which forces a new HTML browser window to open up.
Think back to the PHP script we created earlier. It uses a built in PHP function called "echo" - which does the same as "print" and prints out stuff to an HTML page. We've told it to multiply our number by 7 and then echo the result, so it should print the result of 7*year to the HTML page. Save your fla and lets try it.
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, and if you're testing on a remote server, upload your files to the relevant folders on that server.
9. Let's test this now. Once you can see the Flash interface enter a number - let's say 7 - into the input box and press the button. A new browser window should pop open, and that page should have the number 49 printed to it (7*7=49 for those that struggle with maths!) So, we know that our php script is doing its bit!
We're going to take a look at that PHP script again now:
Receiving variables from Flash
<?
$calculation = $_POST['years']*7;
echo $calculation;
?>
The "<?" is the opening PHP tag. All variables created or referenced in PHP are preceded with a dollar sign. All variables received by a PHP script are automatically stored in handy PHP arrays for us. One such array is called $_POST[] . Now, we sent a variable called years from Flash to PHP, and so using that knowledge we know we can access it like this:
$_POST['years']
As you can see from the PHP scipt, we created a variable called "calculation" and assigned to it the value of $_POST['years'] *7 , and then on the line below that we tell PHP to print that value out to the browser using the "echo" function.
Okay, now we're going to replace one line of code so that our number gets received by Flash instead of being printed out to the browser window.
10. Comment out the line echo $calculation; and replace it with the following:
echo "&returnVal=$calculation";
Again you can see that we're getting PHP to echo a value out to the page, but what about the rest of that line of code?
Well, Flash can receive data in name-value pairs (variables in the format of name=value). If there is more than one pair then each name-value pair is separated by an ampersand, and so a typical string of name-value pairs may look like this:
&name1=Neil&location1=Bristol&name2=Justin&location2=London&
The final ampersand is optional. Here we are building a name-value pair to be sent back to Flash...

