RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Four Ways to Transfer Data Between Flash and PHP : Page 3

Discover the options for implementing bidirectional communications between browser-based Flash forms and server-side PHP code.

From PHP to Flash Using LoadVars
Getting Flash data to PHP is fine if you need to display only blank forms, but for most purposes, you also need to be able to set control values in the Flash form from PHP as well. To send data from PHP to Flash you can use the LoadVars.load method. This method detects when a Flash form receives data from an external program. When all the data has been transferred, Flash calls the LoadVars.onLoad method, which represents the success of the transfer using a Boolean value. The syntax of the load method is:

In the preceding code, the lv_object is an instance of the LoadVars class, while url is the URL from which Flash should download the variable values.

For sending data to Flash you can use the echo/print PHP methods. For each data item use the following syntax:

For example, to send a message to flashForm from form.php add the following code at the end of the script:

   $message='The data values were '. 
      'received from Flash successfully!';
   echo 'Sending to Flash ... ';
   echo "&varMessageFromPHP=$message";
To receive the message in flashForm place this code in the Actions panel of the Submit button, at the end of the on(release) method:

   myData = new LoadVars();
   myData.onLoad = function(success)
           _parent.messageFromPHP.text = 
Figure 2. PHP Message: The sample Flash form displays messages from PHP in a label control.
Figure 2 shows flashForm displaying the message from PHP in a Label component:

Reading Data from a Text File
Another option for sending data from PHP to Flash is to use a text file as an intermediate data buffer. For example, the following PHP code (fileForFlash.php) generates a text file (data.txt) that contains a set of values to pre-populate the flashForm form. After the stream is closed, PHP loads the flashForm form. Notice how you should generate the file so that Flash can parse it correctly:

      "&checkboxes=PHP,Flash,XML&" + 
      list=RSS&observation=Just a text".
        "&message=The data from file".
        "were loaded successfuly!\n");
   echo '<object classid=
     cab#version=7,0,0,0" width="600" 
     height="600" id="flashform" 
   echo '<param name="allowScriptAccess" 
      value="sameDomain" />';
   echo '<param name="movie" 
      value="flashform.swf" />';
   echo '<param name="quality" 
      value="high" />';
   echo '<param name="bgcolor" 
     value="#000000" />';
   echo '<embed src="flashform.swf" 
     quality="high" bgcolor="#000000" 
     width="600" height="600" 
     name="flashform" align="middle" 
       "http://www.macromedia.com/go/getflashplayer" />';
   echo '</object>';
Now you can load the values from the file into PHP using the LoadVars.load method like this (the form is pre-populated when Flash loads the Submit component, because you are using the load event, which takes effect when the form's components are loaded):

      myData = new LoadVars();
      myData.onLoad = function(success){
         //"name" text field

         _parent.name.text = myData.username;
         //"email" text field
         _parent.email.text = myData.useremail;
         //"male"/"female" radio buttons
         if(myData.usersex == "Male")
            _parent.male.selected = true;   
            _parent.female.selected = true; 
         //"country" combobox 
               _parent.country.selectedIndex = l; 
                 {type:"change", target:_parent.country});
         //"checkbox1 .. 4" checkboxes
         var checkboxes:String = (myData.checkboxes).toString();
         if(checkboxes.indexOf("PHP",0) != -1)
            _parent.checkbox1.selected = true; 
         if(checkboxes.indexOf("XML",0) != -1)
            { _parent.checkbox2.selected = true; }
         if(checkboxes.indexOf("Flash",0) != -1)
            { _parent.checkbox3.selected = true; }
         if(checkboxes.indexOf("HTML",0) != -1)
            { _parent.checkbox4.selected = true; }
         //"format" list
              (_parent.format.getItemAt(l)).label) != -1)
               _parent.format.selectedIndex = l;
         //"observation" text area
         _parent.observation.text = myData.observation;
         //message from PHP
         _parent.messageFromPHP.text = myData.message; 
Author's Note: Paste the preceding on(load) method code in the Actions panel of the Submit button. The on(load) method runs when the Submit component gets loaded. Don't overwrite the on(release) method shown earlier.

Figure 3. Pre-Populated Form: Here's the sample Flash form pre-loaded with data from the data.txt file created on the server with PHP.
Figure 3 shows the sample form loaded with the data from the data.txt file.

What's in the Sample Code?
You'll find four zip files in the downloadable sample code that implement the techniques discussed in this article.

  • Example1.zip sends data from Flash to PHP using the getURL method.
  • Example2.zip sends data from Flash to PHP using the LoadVars.send method.
  • Example3.zip sends data from PHP to Flash using the LoadVars.load method.
  • Example4.zip pre-populates the Flash form using an intermediate text file generated by a PHP page. When the application submits the pre-populated values they are sent to another PHP page.
All in all, using Flash and PHP together can be a great solution when you need to combine special designs/effects with strong business logic in professional web applications. Now that you've seen how to transfer data between the two different technologies, you can add Flash-based forms to your PHP applications and make them work together smoothly.

Octavia Andreea Anghel is a senior PHP developer currently working as a primary trainer for programming teams that participate at national and international software-development contests. She consults on developing educational projects at a national level. She is a coauthor of the book "XML Technologies—XML in Java" (Albastra, ISBN 978-973-650-210-1), for which she wrote the XML portions. In addition to PHP and XML, she's interested in software architecture, web services, UML, and high-performance unit tests.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date