Flash & PHP Integration 101 by CyanBlue
Flash & PHP Integration 101
by CyanBlue
 
This is very simple knock down version of how you can send variables from Flash to PHP so that PHP can do something and return the value back to Flash.
 
Take a look at the sample first to see how it is looking when you see the final version.
All you have to do is to insert two numbers into the given textfields and press on the '=' sign to get the result. (Yes, you can do this within the Flash totally fine, but I am just trying to show you some example. That's all.)
 
 

FlashVacuum Flash ActionScript Tutorial CyanBlue subQuark JT

You do not have the Flash plugin installed, or your browser does not support Javascript.

 
 
I am sure you know how this little program works, right? Now, here is the core routine that calls up the PHP script and get the value displayed into the textfield upon receiving the value from the PHP.
 
  1. sum_btn.onPress = function()
  2. {
  3. result_lv = new LoadVars();
  4. result_lv.num1 = num1_txt.text;
  5. result_lv.num2 = num2_txt.text;
  6. trace("Sending... " + result_lv.num1 + " " + result_lv.num2);
  7. result_lv.onLoad = function (success)
  8. {
  9. if (success)
  10. {
  11. total_txt.text = unescape(this.result);
  12. trace("Return value from the PHP : " + unescape(this));
  13. }
  14. else
  15. {
  16. total_txt.text = "ERROR";
  17. trace("Cannot call the PHP file...");
  18. }
  19. }
  20. // result_lv.sendAndLoad("http://localhost/addPHP.php", result_lv, "POST");
  21. result_lv.sendAndLoad("addPHP.php", result_lv, "POST");
  22. }
 
Let's see how this ActionScript works.
 
Line 1   sum_btn.onPress = function()
  This line means that the given function that is defined between line 2 and line 22 are going to get executed when the user clicks on the button instance on the stage called 'sum_btn'.
Line 3   result_lv = new LoadVars();
  Here we are creating a new instance of the Flash's built-in LoadVars() object that takes care of sending and receiving calls to an external script or file.
Line 4 - 5   result_lv.num1 = num1_txt.text;
result_lv.num2 = num2_txt.text;
  Here we are creating two variables within the new instance of LoadVars() object that we have created on line 3.
These variables, num1 and num2, are going to be used to send out the values to the external PHP.
Right now, I am assigning the content of the textfields that are laid on the stage into the num1 and num2 variables.
Mind you that you might need to add some validation capability if you are dealing with stuff like telephone number or zip code or sort.
Line 6   trace("Sending... " + result_lv.num1 + " " + result_lv.num2);
  This line traces out the value that we are sending to the PHP script. I have it to display the vlaues into the output panel within the Flash so that I KNOW that I do not have problem when sending out the variable to PHP. Keep your eyes on to the output panel when testing this file. Make sure that you are seeing two values that you have entered beforehand. That way you know where the problem might be if you cannot get this script working.
Line 7   result_lv.onLoad = function (success)
  Here we are defining an action that is going to be happening WHEN we hear something back from the PHP file that we are about to call. Make sure that you define the onLoad handler BEFORE you call your PHP script in line 20 or 21.
You can notice that there is one argument in the onLoad function called success. That is the way you check whether you have received result from the PHP script or not. You can use any word instead of 'success'. You can use 'ifLoaded' or 'wasItSuccessful' or anything as long as it is one word.
Line 9   if (success)
  and here you can see how I am utilizing the argument that I have supplied in line 7.
Basically what it means is process the line 11 and 12 if you have received something from the PHP script and process the line 16 and 17 if you did not received anything.
Line 11   total_txt.text = unescape(this.result);
  Here we are assigning the text of the textfield instance total_txt with the value that you have received from the PHP script. That word 'this' means the result_lv itself and 'result' should match whatever you have your PHP script set to print out the value back. In this case this is how I am returning the value in PHP.
     echo("result=$rtnValue&received1=$fVar1&received2=$fVar2");
Also, you might want to check out the Flash manul to see what that unescape() function does to see why I am using that function to wrap the incoming value from the PHP. It is not necessary in this case, but it is always a good idea to add it in case you are getting some special characters such as " " or "<" or ">".
Line 12   trace("Return value from the PHP : " + unescape(this));
  This is the same thing as line 12, but I am getting this traced out into the output panel for debugging purpose.
Line 16   total_txt.text = "ERROR";
  Same thing as line 11, but it will display 'ERROR' into the textfield in case we did not receive anything from the PHP script.
Line 17   trace("Cannot call the PHP file...");
  Same thing as line 12 for the debugging purpose.
Line 20   result_lv.sendAndLoad("http://localhost/addPHP.php", result_lv, "POST");
  This is the line that is actually calling the external PHP file called addPHP.php. I prefer using sendAndLoad() function even I am not supposed to receive anything from the external file because it will not open up the new browser window. So, if I have nothing to receive, I just add this line at the end of the PHP script to make sure I get something.
     echo("dummy=ProcessFinished");
Basically I am supplying three arguments to this sendAndLoad() function call. The name of the external script or file that I want to call, the name of the object that is going to store all the values from the given script and the method of calling the external script.
As you can see, I have commented out the script in line 20 and used the script in line 21 to produce the output file. I normally test all the PHP script from within the Flash, and if I were to do it, I'd have to supply the absolute path to the given script file starting with HTTP protocol.
Line 21   result_lv.sendAndLoad("addPHP.php", result_lv, "POST");
  and this is the actual script path that is sitting in the server. You can also supply absolute path to the external script file as long as the script is sitting in the same domain as your SWF file. In other words, if your SWF file is in http://www.domain1.com, then your external script should be existing in the http://www.domain1.com. You cannot call the external script that is sitting in http://www.domain2.com because of the security reason. You can, however, get away from this security restriction by using some work around, but it is not really recommended and out of the scope of this tutorial.
 

Now, here is the PHP script that we have.

 
  1. <?php
  2. $fVar1 = $_POST['num1'];
  3. $fVar2 = $_POST['num2'];
  4. $rtnValue = $fVar1 + $fVar2;
  5. echo("result=$rtnValue&received1=$fVar1&received2=$fVar2");
  6. ?>
 
Line 2 - 3   $fVar1 = $_POST['num1'];
$fVar2 = $_POST['num2'];
  Here we are defining two variables in PHP that we get from the Flash file via POST method. You should really use this $_POST[] array if you have your 'register_globals' setting set to Off in the php.ini file which is the default these days for the security reason.
Line 4   $rtnValue = $fVar1 + $fVar2;
  and, here we are assigning a new varialbe with the sum of those two variables from the Flash.
Line 5   echo("result=$rtnValue&received1=$fVar1&received2=$fVar2");
  This line returns the output back to Flash. Whatever you are printing out from the PHP with echo() or print command will be returned back to Flash. So, it is ideal to have only one line of echo() or print command at the bottom of your PHP script.
The format of the returning string will be the variableName=value and if you have more than one pair, then you can combine them with '&' like this. variableName1=value1&variableName2=value2&variableName2=value2
Actually you only need this line.

     echo("result=$rtnValue");
But I have added two more return values back to Flash for the debugging purpose.
 
It is always a good idea to test your PHP script from the web browser before you call it from the Flash to make sure that your PHP script works as it should be and it tends to reduce good debugging time if you do.
 
Well, that's about it, I guess. See if you can understand what I just said and post the question to the forum if you have any or if you have suggestion/correction.
 
Download the file from here : addPHP.zip
 

 

   |      |      |      |   Last Modified: July 17, 2009 @ 4:25 am