Sending dynamic data to server and back in flash
This tutorial shows you how to send data from flash to php then change the data and send back to flash using LoadVars.
This tutorial shows you how to send data from flash to php then change the data and send back to flash using LoadVars.
The following code would be placed on the frame timeline, setup object to catch data from remote file
var retrieveLoadVars:LoadVars = new LoadVars();
setup object to send data to remote file
var sendLoadVars:LoadVars = new LoadVars();
call function when data is loaded
retrieveLoadVars.onLoad = function(success)
{
if(success)
{
trace(retrieveLoadVars.name1);// print out results to the output window
trace(retrieveLoadVars.name2);// print out results to the output window
} else //data not loaded
{
trace("error");// print out error to the output window
}
}send data to remote file by assigning the data to a variable.
sendLoadVars.param1 = "flash"; sendLoadVars.param2 = "rules";
send data to remote file then load the data. specify url then object to catch loaded data then method to be used.
sendLoadVars.sendAndLoad("http://www.yourdomain.com/filename.php",retrieveLoadVars,"POST");Next to process the data being sent to php:
Add each param to a variable then process it when ready sent it back by echoing it out in named pairs
<?php $name1 = $_POST['param1']; $name2 = $_POST['param2']; if($name1 == 'flash'){ $name1 = 'php';} echo "name1=$name1&name2=$name2"; ?>
That’s it, this example is very simple in a real world example you may want to looking a database and pull a record and send it back to flash when the php file is called.
Here’s the full script
Flash
var retrieveLoadVars:LoadVars = new LoadVars();// setup object to catch data from remote file
var sendLoadVars:LoadVars = new LoadVars(); // setup object to send data to remote file
retrieveLoadVars.onLoad = function(success) // call function when data is loaded
{
if(success) //function to run if data has been loaded
{
trace(retrieveLoadVars.name1);// print out results to the output window
trace(retrieveLoadVars.name2);// print out results to the output window
} else //data not loaded
{
trace("error");// print out error to the output window
}
}
sendLoadVars.param1 = "flash";//send data to remote file
sendLoadVars.param2 = "rules";//send data to remote file
sendLoadVars.sendAndLoad("http://www.yourdomain.com/filename.php",retrieveLoadVars,"POST");
//send data to remote file then load the data. specify url then object to catch loaded data then method to be used.PHP
<?php $name1 = $_POST['param1']; $name2 = $_POST['param2']; if($name1 == 'flash'){ $name1 = 'php';} echo "name1=$name1&name2=$name2"; ?>







Discussion
No responses to "Sending dynamic data to server and back in flash"
There are no comments yet, add one below.
Leave a Comment