04May2011
Author
Dave
Category
Flash
Tags
, , ,
Sending dynamic data to server and back in flash Thumbnail

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";
?>
Author
Dave

About the Author

has written 72 articles on Dave is my name.

My name is David Carr I'm a PHP web developer based in Hull it is my passion to design and develop clean, accessible and usable websites using PHP and other web technologies, I'm an avid Twitter user why not follow me? https://twitter.com/daveismynamecom

Visit this author's website   ·   View more posts by

Sharing is caring.
  • Subscribe to our feed
  • Share this post on Delicious
  • StumbleUpon this post
  • Share this post on Digg
  • Tweet about this post
  • Share this post on Mixx
  • Share this post on Technorati
  • Share this post on Facebook
  • Share this post on NewsVine
  • Share this post on Reddit
  • Share this post on Google
  • Share this post on LinkedIn

Discussion

No responses to "Sending dynamic data to server and back in flash"

There are no comments yet, add one below.

Leave a Comment