Logo

Sending Variables from AS3 to PHP

Posted under » PHP » Flash on 13 March 2009

In this exercise, 2 AS3 classes will be used, namely URLVariables and URLRequest.

In AS3 we use the URLVariables class to store our intended variables like so: first we instantiate the myVariables var with the URLVariables class.

var myVariables:URLVariables = new URLVariables();

Then we create and define properties within the myVariables instance (these are the names we will call in PHP to access the data).

myVariables.myName = "Hanafi";
myVariables.beans = " is a Javanese who cannot program in Java";

Now lets take what we learnt from lesson #1 where we instantiate the myURLRequest var with the URLRequest class.

var myURLRequest:URLRequest = new URLRequest("http://localhost/Flash_samples/phpvarstutorial/phpvarstutorial.php");

Now for the magic that makes it all work…Combining the two objects of myURLRequest and myVariables where myVariables become a property of myURLRequest object.

myURLRequest.data = myVariables;

and do the buttons from lesson #1.

And create the function to navigateToURL.

function clickHandler(event:MouseEvent):void {
	navigateToURL(myURLRequest, '_blank');
}

In the PHP file, I used the $_GET['var']; to retrieve the variables (myName and Beans).

You may find doing the same program in AS2 much easier.

Example files here (PHP included)