Passing Variables Between Flash ActionScript and PHP

In the most of the web based flash applications we required to communicate with server side scripting languages like PHP to send data to database and receive data from database. It is because flash don’t have support to directly read data from database.
In this article I am going to show you , how to pass variables or data between Flash and PHP.
Prerequisites:
To call php url you need web server. You may use any of the hosting which support php script or you can setup web server on your local pc also. Find the documentation on this link to setup WAMP. http://www.wampserver.com/en/

Passing Variable from Flash to PHP

To pass variables from Flash to PHP first we have to define URLRequest and URLLoader to load URLRequest. See below.
[as3]
var url:String = “http://ajaybadgujar.com/uploads/2013/11/passvars/username.php”;
_request = new URLRequest(url);
_urlLoader = new URLLoader();
[/as3]
Now define URLVariables object and set all the variables which you want to send to PHP using GET method. To know more about GET and POST Methods, visit http://www.php.net/
[as3]
var _vars:URLVariables = new URLVariables();
_vars.id = 1;
_request.data = _vars;
_request.method = URLRequestMethod.GET;
[/as3]
Now load the request and add Event.COMPLETE and IOErrorEvent.IO_ERROR event listeners to URLLoader object.
[as3]
_urlLoader.load(_request);
_urlLoader.addEventListener(Event.COMPLETE, onComplete);
_urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onError);
[/as3]
The complete url will look like this with variables.
“http://ajaybadgujar.com/uploads/2013/11/passvars/username.php?id=1”

GETing passed data from flash to PHP

See the code below, how to get data and pass the values to flash using url variables.

Receive data from PHP to Flash

Inside the onComplate(e:Event) function receive result data.
[as3]
private static function onComplete(e:Event):void {
_urlLoader.removeEventListener(Event.COMPLETE, onComplete);
_urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, onError);
vars = new URLVariables(e.target.data);
trace(“Name : ” + vars.fname + ” ” + vars.lname);
}
[/as3]

Complete Source

See the class Illustrated below named “VARLoader”.
[as3]
package com.ajaybadgujar.dataloader{
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLVariables;
public class VARLoader {

public static var onSuccess:Function = null;
public static var onFailure:Function = null;

public static var _request:URLRequest = null;
public static var _urlLoader:URLLoader = null;

public static function loadData(_fileName:String, _vars:URLVariables, _onSuccess:Function, _onFailure:Function):void{

onSuccess = _onSuccess;
onFailure = _onFailure;

trace(‘Data load prepairing’);
var url:String = _fileName;
_request = new URLRequest(url);
_urlLoader = new URLLoader();
_request.data = _vars;

_urlLoader.dataFormat = URLLoaderDataFormat.TEXT;

_urlLoader.load(_request);
_urlLoader.addEventListener(Event.COMPLETE, onComplete);
_urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onError);

}

private static function onComplete(e:Event):void {
_urlLoader.removeEventListener(Event.COMPLETE, onComplete);
_urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, onError);
onSuccess(new URLVariables(e.target.data));
trace(“Data loading success”);
}

private static function onError(e:IOErrorEvent):void{
trace(‘Connection Error’);
onFailure();
}

}

}
[/as3]

How to use

[as3]
package{
import flash.display.Sprite;
import flash.net.URLVariables;
import com.ajaybadgujar.dataloader.VARLoader;

public class Main extends Sprite{

var url:String = “http://ajaybadgujar.com/uploads/2013/11/passvars/username.php”;

public function Main() {
var vars:URLVariables = new URLVariables();
vars.id = 1;
VARLoader.loadData(url, vars, DisplayName, LoadingNameFail);
}

private function DisplayName(vars:URLVariables):void{
trace(“Name : ” + vars.fname + ” ” + vars.lname);
}

private function LoadingNameFail():void{
trace(“Loading name failed”);
}

}

}
[/as3]

Result:

Passing Variables Between Flash ActionScript and PHP

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *