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)){ echo "confirmStatus=success&confirmName=$senderName&confirmMessage=The email was sent successfully."; }else{ echo "confirmStatus=failed&confirmName=$senderName&confirmMessage=There was an error in sending your email."; }

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.

1.

Open up confirmMail.fla and create a text field next to your send_btn with the following properties:

  • Type Dynamic

  • Font size 12

  • Bold Style true

  • Instance name confirm_txt

2.

In the Actions panel of the first frame, add this code to the bottom:

 //When the object receives data back from the PHP page sendMail_lv.onLoad = function(success){     if(success){         if(this.confirmStatus == "success"){             confirm_txt.textColor = 0x006600;             confirm_txt.text = "Congratulations " + this.confirmName + "." + newline;             confirm_txt.text += this.confirmMessage;         }else{             confirm_txt.textColor = 0x990000;             confirm_txt.text = "Sorry " + this.confirmName + "." + newline;             confirm_txt.text += this.confirmMessage;         }     } } 

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.




Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net