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

Page 4 of 5
Sending variables from PHP to Flash
We've seen how variables sent from Flash can be accessed in PHP, but to send a variable back from PHP to Flash we use a slightly different method.
If Flash is expecting a value to be returned (for more info read about sendAndLoad on the next page) then it will pick up this returned value and we can use it in our Flash movie. We still use the PHP language construct echo to output our value from PHP, but we echo it in "name-value pair" format so that Flash can understand it, instead of a straight-forward variable value as we did earlier.
When sending back data from PHP to Flash it is often the case that we need to encode it properly using PHP's urlencode() method, but in this case, because we are just sending back a number it isn't necessary to encode the data in any way.
You may have also noticed that we did something in that last bit of PHP (on the previous page) that wouldn't be allow in Flash - we put a variable inside quotation marks! In PHP all variables are denoted by a dollar sign, making them easily distinguishable from normal strings. In PHP anything inbetween double quotes is evaluated. Therefore this is perfectly acceptable, and even though the variable is inside quotes it still gets evaluated:
<?
$myNumber = 10;
echo "My number is $myNumber"; //prints "My number is 10"
?>
So, putting it all together, we know we need to echo something back to flash in name-value pair format - and for the name part we're just going to chose an appropriate variable name , - "returnVal" sounds good to me! This needs to be echoed as a string. Therefore, so far we have this:
echo "&returnVal="
For the value part we want to send back our PHP variable called $calculation. We know that we can put variables inside of double quoted strings so...
echo "&returnVal=$calculation";
Viola!
Okay, we're on the home straight now. We need to go back to our Flash movie, and make a few changes so that Flash will be able to pick up and use the name value pair that we are sending back. We need to modify our actionscript code to do this. We also need somewhere to display the result.
10. Create a dynamic textbox on stage, make it multiline and wordwrap and give it the instance name of output .
11. Now replace ALL the code in frame 1 with this, and I will explain the additional code below.
stop();
//=================================
// INIT
//=================================
path = "../nwjv/php/"; //declare path to php files
lvOut = new LoadVars(); //create lv object
lvIn = new LoadVars(); //create lv object
lvIn.onLoad = function (success) {
if(success){
//PHP variable value to textbox
output.text = lvIn.returnVal;
}else{
//...or notify of failure
output.text = "fail";
}
}
//=================================
// BTN
//=================================
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");
//get results returned to lvIn
lvOut.sendAndLoad(path + "dogyears.php", lvIn, "POST");
};
We'll examine this code line by line starting with the INIT code. First we store our path, as a string in a variable called path . Next we create two loadVars objects. One object will be sent from Flash to PHP, and the other object receives the value sent back from PHP to Flash.
Next we use the onLoad method of our LoadVars object to tell Flash what to do if the load is successful (I've explained very similar methods in many of my other tutorials and so I will not go in to too much detail here, but basically if the sendAndLoad is successful our returnVal variable will have been sent from PHP to FLASH, and will automatically be stored inside our loadVars object called lvIn (see BTN script for more info) - and therefore we assign lvIn.returnVal to our Flash textbox. If the operation isn't successful we display a "fail" message in the textbox instead).
We'll take a look at the button code on the next page.

