LoadVars class is the most powerfull class for communicating with server-side technologies and to load external data from text files. It was introduced in Flash Player 6 to provide a cleaner, much more object-oriented interface for the common task of exchanging data with a server and text files.
LoadVars class is an alternative to the loadVariables action with more flexibility for transferring variables between a Flash movie and a server-side technologies like ASP, ASP.NET, PHP, JSP, ColdFusion, CGI-Perl, etc.
You can use the LoadVars class to obtain verification of successful data loading, progress indications, and stream data while it downloads. The LoadVars class works much like the XML class; it uses the methods load, send, and sendAndLoad to communicate with a server. The main difference between the LoadVars class and the XML class is that LoadVars transfers ActionScript name and value pairs, rather than an XML DOM tree stored in the XML class. It also lets you to send variables only that are required by the server instead of sending all the variables as in the case of loadVariables or loadVariablesNum.
Advantages of the LoadVars class include the following:
• You don't need to create container movie clips for holding data or clutter existing movie clips with variables specific to client/server communication.
• The class interface is similar to the XML object, which provides some consistency in ActionScript. It uses the methods load(), send(), and sendAndLoad() to initiate communication with a server. The main difference between the LoadVars and XML classes is that the LoadVars data is a property of the LoadVars object, rather than an XML Document Object Model (DOM) tree stored in the XML object.
• The class interface is more straightforward—with methods named load, send, sendAndLoad—than the older loadVariables interface.
• You can get additional information about the communication, using the getBytesLoaded and getBytesTotal methods
• You can get progress information about the download of your data.
• The callback interface is through ActionScript methods (onLoad) instead of the obsolete, deprecated onClipEvent (data) approach is required for loadVariables.
• There are error notifications to track errors in communication.
• You can add custom HTTP request headers.
• You must create a LoadVars object to call its methods. This object is a container to hold the loaded data.
Using LoadVars Object for communicating with the server:
The following are the five basic steps involved in using LoadVars Object for communicating with any server:
1)Instantiate two LoadVars objects, one for sending variables from flash and one to receive any variables that are sent from the server
2)Define and associate the flash variables to LoadVars object that are required to send to server
3)Use sendAndLoad method to send the associated variables to server as POST/GET variables and also specify the LoadVars object for receiving variables from server
4)Use onLoad event handler method to assign a function which fires when the variables are received from the server. The onLoad handler is passed a true or false Boolean value indicating if the data was submitted successfully.
5)Create a function with the name specified in step 4 to handle the received variables. The function will be called when the server's reply has been received. You need to write this function yourself.
Let us see an example now to demonstrate the above steps in ActionScript 2.0
Step 1
var reply_lv = new LoadVars();
var send_lv = new LoadVars();
Step 2
send_lv.type = "BUTTON";
send_lv.id = "1";
Step 3
send_lv.sendAndLoad("asp_net_php_page", reply_lv, "POST");
Step 4
reply_lv.onLoad = loadedDotNetVars;
Step 5
function loadedDotNetVars(success){
if(success) {
// operation was a success
trace(reply_lv.name)
}
else {
// operation failed
}
}
If the sendAndLoad operation finishes without errors, any variables returned by the server will be placed into reply_lv and the loadedDotNetVars function will be invoked.
Note: ‘name’ is the variable name that was specified in the server-side script
If the http send method was set to GET all the variables will be sent to the server in query string format like:
type=BUTTON&id=1
Suppose if you are sending arrays to the server those array values are sent as a comma-delimited list
Ex: If you are sending the following data
send_lv.type = "BUTTON";
send_lv.id = "1";
send_lv.colors = ["red", "white", "blue"];
the query string will look like:
type=BUTTON&id=1&colors=red,white,blue
Receiving the variables in server-side script:
You can access the flash variables in the server-side script normally like you access form POST/GET variables. The variable names will be the names you use when associating the variables to the LoadVars Object.
Example 1 (ASP): Request(“type”);
Example 2 (PHP): $_POST['type']
Example 3 (ColdFusion): #FORM. type#
We hope the information helped you. If you have any questions
or comments, please don't hesitate to post them on the
Forums section Submit your Tutorial at Click Here