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:
lv_object.load(url)
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:
&flash_variable_name=$php_variable_name;
For example, to send a message to
flashForm from
form.php add the following code at the end of the script:
<?php
$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.load("form.php");
myData.onLoad = function(success)
{
if(success)
{
_parent.messageFromPHP.text =
myData.varMessageFromPHP;
}
}
 | |
| 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:
<?php
$hf=fopen("data.txt","w");
fwrite($hf,"&username=Mike".
"&useremail=mike@org.com".
"&usersex=Female".
"&usercountry=Spain".
"&checkboxes=PHP,Flash,XML&" +
list=RSS&observation=Just a text".
"&message=The data from file".
"were loaded successfuly!\n");
@fclose($hf);
echo '<object classid=
"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
codebase="http://download.macromedia.com/pub/
shockwave/cabs/flash/swflash.
cab#version=7,0,0,0" width="600"
height="600" id="flashform"
align="middle">';
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"
allowScriptAccess="sameDomain"
type="application/x-shockwave-flash"
pluginspage=
"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):
on(load)
{
myData = new LoadVars();
myData.load("data.txt");
myData.onLoad = function(success){
if(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;
}
else
{
_parent.female.selected = true;
}
//"country" combobox
for(l=0;l<_parent.country.length;l++)
{
if((_parent.country.getItemAt(l)).label==myData.usercountry)
{
_parent.country.selectedIndex = l;
_parent.country.dispatchEvent(
{type:"change", target:_parent.country});
break;
}
}
//"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
for(l=0;l<_parent.format.length;l++)
{
if((myData.list).indexOf(
(_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.