PHP and FlashReceiving Data
As stated before, integrating Flash with PHP will allow us to create some pretty dynamic applications. We aren't quite there yet, so let's start with some simple examples of how Flash can receive data from PHP.
Example 1Hello World
In our first example we will use the most common of examples, displaying the text Hello World. Here our PHP file will already contain the variable and send it to Flash to be displayed. Create a new PHP file, save it as helloWorld.php , and paste the following script inside:
See a difference? In this example, not only are we displaying "Hello World," but we are saying that it is the value of "myVariable." The reason for this is that Flash will understand only the data in the name/value pair format. Now that we have the PHP script created, let's move on to our Flash file.
<?php
echo "myVariable=Hello World";
?>
1. | Create a new Flash file, save it as helloWorld.fla , and save it to the same directory as helloWorld.php on your server. |
2. | In the Actions panel of the first frame, place this code inside:
|
Walking through the ActionScript, we can see that we have created a dynamic text field in the top-left corner of the canvas. We then create an instance of the LoadVars object, which will be used to connect to the PHP page and receive the data. The onLoad event is then created to tell Flash what to do when the requested data is returned. In it we test the value of success to see if there were any issues connecting to our PHP page. If success returns true, it will write the myVariable value to our text field, and if not it will display "An error has occurred" in the Output panel. Now all we have to do is make the request using the load method by passing it the full path to your helloWorld.php file.Publish your movie, and if you run the generated SWF you should see the following result, as shown in Figure 22.1:
Figure 22.1. The result of our "Hello World" example.

Example 2Hello, Goodbye World
In this example we will be passing two variables instead of one. These variables will be displayed in different text fields.
1. | Create a new PHP file, save it as helloGoodbye.php , and paste the following script inside:As you can see, the PHP script is identical to example1 except for one small thing: We are now passing two variables instead of one. To pass numerous variables, we place an ampersand (&) between the name/value pairs. |
2. | Create a new Flash file, save it as helloGoodbye.fla , and save it to the same directory as helloGoodbye.php on your server. |
3. | In the Actions panel of the first frame, place this code:
|
Walking through the ActionScript, you can see that we are now creating two dynamic text fields in the top-left corner of the canvas, 20 pixels apart. Notice that we place them on separate levels; otherwise, the next field will wipe out the preceding field. After success returns true, we will write the two variables we have received to their respective fields. Now all we have to do is make the request using the load method by passing it the full path to your helloGoodbye.php file.Publish your movie, and if you run the generated SWF, you should see the result shown in Figure 22.2:
Figure 22.2. The result of our "Hello, Goodbye World" example.

Example 3Multiple Values and Multiple Variables
In this example, we will work with multiple variables, like example 2. The difference is that the variables in this example will have multiple values. Instead of using a name/value pair for each variable, we can place multiple values into one variable.In this example we have three people. Each person has a name, age, and weight. If we did this with individual variables it would take a total of nine variables. Instead we pass three variables, one for name, one for age, and one for weight, and let Flash do the work of parsing the values. Create a new PHP file, save it as multipleValues.php , and paste the following script inside:
Because we are working without a database, our information is currently stored in arrays. We cannot pass these arrays directly to Flash, so we loop through them and store the data as a string into new variables, separated by the tilde (~) character. Finally, we write the information to the browser. If you test this page alone on your server, you should see what is shown in Figure 22.3:
<?php
//Arrays that hold the values
$personName = array("Rick", "Jane", "Andy");
$personAge = array("12", "13", "11");
$personWeight = array("115", "103", "105");
//Loop through the array and store the values in new variables
for($i=0;$i<count($personName);$i++){
$sentName .= $personName[$i] . "~";
$sentAge .= $personAge[$i] . "~";
$sentWeight .= $personWeight[$i] . "~";
}
//Write the results to the screen
echo "sentName=$sentName&sentAge=$sentAge&sentWeight=$sentWeight";
?>
Figure 22.3. Data written to the browser.
[View full size image]

1. | Create a new Flash file, save it as multipleValues.fla , and save it to the same directory as multipleValues.php on your server. |
2. | In the Actions panel of the first frame, place this code:
|
The ActionScript for this example is slightly different from the previous examples. First, we create the text fields, but because they are going to be holding multiple strings, we make them multiline. We also give them a border so we can see their size and placement. Next we use the LoadVars object, but the difference is how we handle the data that is returned. First we create three arrays and store the incoming data into those arrays by splitting the values based on the ~ character. Then we look through the arrays and populate the text fields with the values returned. Finally, we call the PHP page to get the process going. If you test the movie, you should see the results shown in Figure 22.4:
Figure 22.4. The text fields populated with the parsed data.

Example 4Sending Mail with Flash and PHP
The first three examples have no real-world application, but they give you a basis for understanding how to integrate Flash and PHP. This next example will show you how to send a simple email using Flash as the interface and using PHP to process the email. Create a new PHP file, save it as sendMail.php , and paste the following script inside:
As you can see, the script is very straightforward. This page will first accept the five variables passed to it:
<?php
//Get the external variable
$destMail = $_REQUEST["destMail"];
$senderName = $_REQUEST["senderName"];
$senderMail = $_REQUEST["senderMail"];
$senderSubject = $_REQUEST["senderSubject"];
$senderBody = $_REQUEST["senderBody"];
//Add header information
$header = "From: $senderName <$senderMail>\n";
$header .= "Reply-To: $senderName <$senderMail>\n";
//Send Email (to, subject, body, header)
mail($destMail, $senderSubject, $senderBody, $header);
?>
- destMail
Destination email address - senderName
Name of the sender - senderMail
Email address of the sender - senderSubject
Subject of the email - senderBody
Body of the email
Then we format the header information for the email (the sender's name and email address) and use the mail function to send the email. Now we can move on to the Flash part of this example. We need to create a file that will allow a user to enter in the five pieces of information and submit them to the page we just created.
You may format, label, and color your file however you like. Your basic layout should look, in some way, like Figure 22.5 :
Figure 22.5. The mail interface laid out and labeled.

//If all fields are filled enable button
fieldsFilled = new Object();
fieldsFilled.onKeyUp = function(){
if(destMail_txt.text != '' && senderName_txt.text != '' && senderMail_txt.text !=

send_btn.enabled = true;
} else {
send_btn.enabled = false;
}
}
//Apply the listener, and disable the button on init
Key.addListener(fieldsFilled);
send_btn.enabled = false;
//The object for communicating with the PHP page
var sendMail_lv:LoadVars = new LoadVars();
send_btn.onRelease = function(){
//set the variables we are sending
sendMail_lv.destMail = destMail_txt.text;
sendMail_lv.senderName = senderName_txt.text;
sendMail_lv.senderMail = senderMail_txt.text;
sendMail_lv.senderSubject = senderSubject_txt.text;
sendMail_lv.senderBody = senderBody_txt.text;
//send and receive the data
sendMail_lv.sendAndLoad("http://PATH_TO_YOUR_SERVER/sendMail.php", sendMail_lv, "POST");
}
In the preceding code, we start with the field requirements. Because we need all five pieces of the information, we create a listener (fieldsFilled) that checks to see whether all field fields are not empty. If they all contain some sort of text, then send_btn is enabled. We then create the LoadVars object (sendMail_lv) that we will use to pass the variables. Then we create a function and attach it to the onRelease event of send_btn. This function grabs the values of the five text fields, places them in the LoadVars object, and then calls the sendAndLoad event to pass the data to our PHP page.Now that you have a good understanding of how to send data to PHP from Flash, let's work on PHP sending some data back to Flash.