URLRequest and it’s variable ways

Sending variables to PHP can be a bit more than expected. There are many things to keep in mind when sending and/or receiving data to PHP:

  • URLRequest: this is the main core (1 of 2) in sending data. 
  • URLLoadder: This is the other core (2 of 2). After you set up your URLRequest, you need something to send it….if you come from AS2 background, think of this as “kinda like” LoadVars. NOTE: you are sending “data”, so therefore, your this class conviently has a “data” object…you access this by calling your URLLoader object, and then it’s data property: var dt = myurl_loader.data. Really, you only need this if you are planning to send AND receive data….otherwise, the URLRequest combined with sendToURL() function is enough
  • How you are sending your data: URLRequestMethod (GET vs POST).
  • URLLoaderDataFormat.VARIABLES- this is where you store your unique variables . It’s an object really, that becomes the variable of the URLRequest.data object. So, you can have “request.data=” your new URLVaraiables object.
all said, a quick snippet to just sending data (both examples use the default URLRequestMethod.GET):
//SEND DATA
//-------------------------------------->>
private  function sendItAll():void
{
var obj:URLVariables= new URLVariables();
obj.first_name="myfirstname";
obj.last_name="mylastname";
var url:String = "http://www.website.com/script.php";
var request:URLRequest = new URLRequest(url);
request.data = obj;
try
{
sendToURL(request);
}
catch (e:Error)
{
trace('there is an error here')
}
}

if you want to send AND receive data, we have different players in the game. You will need to add an Event Listener to retrieve the data after the load:

var request:URLRequest = new URLRequest();
request.url= "http://www.site.som/script.php";
var urll:URLLoader = new URLLoader();
urll.addEventListener(Event.COMPLETE, complete_handler);
urll.load(request);
private function complete_handler(e:Event):void
{
var urll:URLLoader = URLLoader(e.currentTarget);
var returnedData= urll.data;
}