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!
Here is a strange thing. At one point the input fields were set to multiline. This caused really
odd behaviour when the tab key was pressed. Text disappeared out of view and the whole
application behaved in an odd way causing apparent lock-ups. Deleting the "invisible" text fixed
the problem. Setting the fields back to Single line fixed this strangeness.
// ====================================================================
// ==== This is the AJAX style bit. The data is posted and received ===
// ====================================================================
var loader : URLLoader = 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 msg : String = event.target.data.msg;
var fname : String = event.target.data.fname;
var lname : String = event.target.data.lname;
// ====================================================================
// ==== Run this code in response to a click on the Go movie clip =====
// ====================================================================
goButton.addEventListener(MouseEvent.CLICK, goButtonClick);
function goButtonClick(e:MouseEvent):void
{
// ==== POST VARIABLES For example ... fname=Fred&lname=Bloggs
var variables : URLVariables = new URLVariables();
variables.fname = firstName.text; // What you entered into the firstName text field
variables.lname = lastName.text; // What you entered into the lastName text field
// ==== Set up the php page URL that does the AJAX stuff. It returns the results in another query string.
var myRequest : URLRequest = new URLRequest();
myRequest.url = "http://www.softwareforeducation.com/tutorials/ActionScriptCS3/99AjaxTingX.php";
myRequest.method = URLRequestMethod.POST;
myRequest.data = variables;
try
{
loader.load(myRequest); // This sends the data to the php page.
}
catch(error:Error)
{
trace("Could not load the remote script page.");
}
}
// ====================================================================
Here is the PHP code processed by the server in 99AjaxTingX.php
// You may need to trim the POST variables to remove spurious and unpredictable
// whitespace and newline characrers which cause Flash to do strange things.