User Guide

Sending and loading variables to and from a remote source 283
You can test this code by using a user name of JeanSmith and the password VerySecret. The
first section of the script generates the following XML when the user clicks the login button:
<login username="JeanSmith" password="VerySecret" />
The server receives the XML, generates an XML response and sends it back to the SWF file. If the
password is accepted, the server responds with the following:
<loginreply status="OK" session="46597865D0B7884E007DEA7D" />
This XML includes a session attribute that contains a unique, randomly generated session ID,
which will be used in all communications between the client and server for the rest of the session.
If the password is rejected, the server responds with the following message:
<loginreply status="FAILURE" />
The loginreply XML node must load into a blank XML object in the SWF file. The following
statement creates the XML object
loginreplyXML to receive the XML node:
// Construct an XML object to hold the server's reply
var loginReplyXML:XML = new XML();
loginReplyXML.onLoad = function(success:Boolean) {
The second statement in this ActionScript defines an anonymous (inline) function, which is
called when the
onLoad event triggers.
The login button (
login_btn instance) is used to send the user name and password as XML to
the server and to load an XML response back into the SWF file. You can use the
sendAndLoad()
method to do this, as shown in the following example:
loginXML.sendAndLoad("http://www.flash-mx.com.com/mm/main.cfm",
loginReplyXML);
First, the XML-formatted data is created, using the values that the user inputs in the SWF file,
and that XML object is sent using the
sendAndLoad method. Similar to data from a
loadVariables() function, the loginreply XML element arrives asynchronously (that is, it
doesnt wait for results before being returned) and loads into the
loginReplyXML object. When
the data arrives, the
onLoad handler of the loginReplyXML object is called. You must define the
loginReplyXML function, which is called when the onLoad handler triggers, so it can process the
loginreply element.
Note: This function must always be on the frame that contains the ActionScript for the login button.
If the login is successful, the SWF file progresses to the welcome frame label. If the login is not
successful, then the playhead moves to the
loginfailure frame label. This is processed using a
condition and case statement. For more information on case and break statements, see
case and
break in Flash ActionScript Language Reference. For more information on conditions, see if and
else in Flash ActionScript Language Reference.
Note: This design is only an example, and Macromedia can make no claims about the level of security
it provides. If you are implementing a secure password-protected system, make sure you have a good
understanding of network security.
For more information, see Integrating XML and Flash in a Web Application at
www.macromedia.com/support/flash/interactivity/xml/ and the “XML class” entry in Flash
ActionScript Language Reference.