ActionScript 3.0 AJAX Demonstration

Navigation - Site Root :: Tutorial Home
Basics - Hello World :: Text IO :: Calculator :: If Statement
Games - Mouse Tracker :: Ball Bounce :: Maze :: Pong :: Ajax Style Demo
Techniques - Container Objects

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!

Download the FLA file.

Run this example

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

 echo "foo=\"Hello $foo!\"&baa=\"Yo $baa!\"&wee=\"Hay, Dude, $wee!\"";
?>

 

 

What Next?

Once you can pass data to a php script running on the server, and get a reply, the sky's the limit.

The php script can interact with a MySQL or other database server.

ActionScript and Flash overcome the HTML text and static image limitations.

It can be more difficult to make Flash accessible to users with screen readers.