PHP and FlashSending and Receiving Data
As you can see, receiving data can open up a lot of doors to the capabilities of your Flash projects. Still, this only touches on the extent of integrating server-side scripting. When users interact with a web page or an application they often enter data to get certain results. The most common example of this interaction is a search engine: users enter their request, the request is sent to the server, and the server provides the users with data based on the specific information they requested.
Example 5Confirming sentMail
Example 4 showed you a real-world application of sending variables from Flash to PHP by creating and sending a basic email. In this example, we take that a step further by having PHP confirm that the email was sent. This confirmation will be sent to and displayed by Flash. By doing this we can show how to send data back and forth between Flash and PHP.
1. | Duplicate the files you have for our sendMail example and rename each of them confirmMail . |
2. | Open up confirmMail.php and replace the last line of the script, the mail function call, with the following code:[View full width]if(@mail($destMail, $senderSubject, $senderBody, $header)){ |
This script modification does a few things. First, when it calls the mail function, it tests the variables being passed to it. Then, based on the success of the mail function call, it echoes a multiple variable string back to the browser. This string contains three variables:
- confirmStatus
Either "success" or "failed" to let Flash know whether the function call was completed - confirmName
The senderName variable - confirmMessage
A generic message stating the status of the action executed
Now we move on to the modification to our Flash file.
The preceding addition uses the onLoad event of the LoadVars object to test whether the data from PHP has been received. If so, based on the value of confirmStatus, the script formats the variables confirmName, confirmMessage and places them in the confirm_txt text field. It also sets the field's text color to add emphasis.We now have a good example of Flash and PHP sending data to each other. This should give you an idea of how powerful Flash and server-side scripting can be. It also gives us a good basis for our next exampledrawing data from a MySQL database to be displayed in Flash.