Create a text file in your text editor and save it as "phpVariableFile.php." Type in the code below in the text file to create two PHP variables, "themessage" and the "theother," and store in these variables different text messages. Use the PHP "print" function to assign the value of the PHP variables (the text messages) to AS3 variables with the MIME variable format.
<?php
$themessage = "First Message";
$theother = "Second Message";
print "&variable1=$themessage";
print "&variable2=$theother";
?>
The code above uses the MIME variable format in the print function to create a AS3 variable named "variable1" that stores the content of the value of the PHP variable named "themessage."
Save the PHP file and upload it to your Web-hosting service account.
Start the Flash program. Click "Flash File(ActionScript 3.0)" from the splash window to create a new file for the AS3 Flash PHP variable-passing program.
Select "Actions" from the "Window" menu on the main Flash menu bar to open the ActionScript 3 editor. Position your mouse cursor on the first line of the ActionScript 3 editor. Click your mouse button, and type in the code below to create two text boxes on the Flash stage that will be used to display the text values stored in the PHP variables.
var php_variable_1_value:TextField = new TextField();
php_variable_1_value.width = 200;
php_variable_1_value.height = 20;
php_variable_1_value.x = 200;
php_variable_1_value.y = 100;
php_variable_1_value.border = true;
addChild(php_variable_1_value);
var php_variable_2_value:TextField = new TextField();
php_variable_2_value.width = 200;
php_variable_2_value.height = 20;
php_variable_2_value.x = 200;
php_variable_2_value.y = 140;
php_variable_2_value.border = true;
addChild(php_variable_2_value);
Type the code starting at the next line of the ActionScript 3 editor to create a red rectangular button named "request_data_button," and place it on the Flash stage.
var request_data_button:MovieClip = new MovieClip();
request_data_button.graphics.lineStyle(4);
request_data_button.graphics.beginFill(0xFF0000);
request_data_button.graphics.drawRect(20, 100, 50, 20);
request_data_button.graphics.endFill();
addChild(request_data_button);
Type the code starting at the next line of the ActionScript 3 editor to add an event listener to the red button, such that when clicked, the Flash program will access the URL location to which the phpVariableFile.php file was uploaded and retrieve the variable data specified in the PHP print function in the PHP file.
request_data_button.addEventListener(MouseEvent.CLICK, request_data_fn);
function request_data_fn(event:MouseEvent):void {
var dummy_variable:URLVariables = new URLVariables();
var send_request:URLRequest = new URLRequest("http://www.yourdomain.com/phpVariableFile.php");
send_request.method = URLRequestMethod.POST;
send_request.data = dummy_variable;
var store_dummy_variable:URLLoader = new URLLoader;
store_dummy_variable.dataFormat = URLLoaderDataFormat.VARIABLES;
store_dummy_variable.addEventListener(Event.COMPLETE, send_variables_to_AS3);
dummy_variable.dummy = "dummy";
store_dummy_variable.load(send_request);
function send_variables_to_AS3(event:Event):void{
var phpVar1 = event.target.data.variable1;
var phpVar2 = event.target.data.variable2;
php_variable_1_value.text = phpVar1;
php_variable_2_value.text = phpVar2;
}
}
The code above uses a dummy variable whose only use is to activate the PHP program. Once Flash detects that it has made contact with the Web server and associated PHP file, it will place the contents of the PHP variables into the text boxes created. The coding requires that the variable names targeted in the "event.target" statement have the same name as specified in the PHP print function statement.