This example demonstrates AJAX style processing.
Since
there is no XML data or Java code, it really
needs a new
name! How about "Asynchronous ActionScript XML" or AASX.
As this
example uses no XML lets just call it AAS!
ActionScript 3.0 Code for AJAX
Style Processing
without XML and
also without Java.
What should
that be called? "AAS" perhaps.
// ============================================================
// we are going to POST some variables something like this ....
// foo=Lemming&baa=Aardvark&wee=Zebra
// ============================================================
var variables : URLVariables;
variables = new URLVariables();
variables.foo = "Lemming";
variables.baa = "Aardvark";
variables.wee = "Zebra";
// ============================================================
// A PHP script on a web page will process the POST variables
// ============================================================
var myRequest : URLRequest;
myRequest = new URLRequest();
myRequest.url = "http://www.softwareforeducation.com/tutorials/ActionScriptCS3/99AjaxTingX.php";
myRequest.method = URLRequestMethod.POST;
myRequest.data = variables;
// ============================================================
// This is the AJAX bit that listens for the reply
// It is not using XML so perhaps it should be called AJA // Come to that, it's not using Java either so lets call it "A"
// ============================================================
var loader : URLLoader;
loader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, completeHandler);
// ============================================================
// Runs when the reply from the external script is complete
// ============================================================
function completeHandler(event:Event):void
{
var foo : String;
var baa : String;
var wee : String;
foo = event.target.data.foo;
baa = event.target.data.baa;
wee = event.target.data.wee;
replyText.text = foo + "\n" + baa + "\n" + wee;
}
// ============================================================
// Run this code in response to a click on the Go movie clip
// ============================================================
goMC.addEventListener(MouseEvent.CLICK, doGoMC);
function doGoMC(e:MouseEvent):void
{
try
{
loader.load(myRequest);
}
catch (error:Error)
{
trace("Could not load the remote script page.");
}
}
// ============================================================
Here is the PHP code processed by the server
<?php
extract($_POST); // expecting $foo, $baa and $wee