Flashadvisor logo
:: Desktop Shortcut
:: Flash Help
Advices from Experts

 Submit Here! · Link to Us · Newsletter · Tell a Friend ·

      Add Tutorial |

 

Advertise here










 

Google

Search WWW   
FlashAdvisor.com

 Home > Tutorials  > ActionScript

LoadVars Class in Flash

Author: Adi Reddy Mora | Email
Website : www.adiinteractive.com


Advertisement


Sending variables back to flash from server:

You can also send variables back to flash from the server. You just need to print the data on the server-side page for flash to read that data in the following format.


&name=John&id=342&ecode=e123


In the above string name, id and ecode are the variable names that can be used in flash to read the data. You can pass as many variables as you require by seperating them with ampersand (&) symbol.


Example 1 (ASP):
You can use Response object to write the data in ASP
Response.Write("&name=John&id=342&ecode=e123")

Eample 2 (PHP):
You can use print or echo language constructs to write data in PHP.
echo "&name=John&id=342&ecode=e123";

Eample 3 (ColdFusion):
You can use tag to output data to flash.
&name=John&id=342&ecode=e123


Other methods of LoadVars Object for communicating with the server:

LoadVars class consists of three methods for communicating with the server.
LoadVars.load
LoadVars.send
LoadVars.sendAndLoad

We have seen the usage of LoadVars.sendAndLoad method for sending and receiving data to and fro from the server. Similarly based on your requirements if you want to only receive the data from the server you can use LoadVars.load method with the parameter URL to load data from the server.


LoadVars.load("asp_net_php_page");


If you want to only send data to the server you can use LoadVars.send method.


LoadVars.load("asp_net_php_page", "_blank", "POST");


Using LoadVars Object for loading data from text files:

LoadVars Object can also be used to load data from text files.
The following are the four basic steps involved in using LoadVars Object for loading text from .txt files into flash:
1)Instantiate a LoadVars object
2)Use load method to load the text file data into LoadVars object
3)Use onLoad event handler method to assign a function which fires when the variables are received from the text file
4)Create a function with the name specified in step 3 to handle the received variables
Let us see an example now to demonstrate the above steps in ActionScript 2.0


Step 1
var load_lv = new LoadVars();
Step 2
load_lv.load("textfile.txt");
Step 3
load_lv.onLoad = loadTextVariables;
Step 4
function loadTextVariables(success){
if(success){
trace(this.name);
}
}


The text file should contain the data in the following format:


&name=John&id=342&ecode=e123


Hidden Method of LoadVars class:

There is a hidden method available in LoadVars class which was not documented in flash documentation.


LoadVars.decode


This method can be used to internally decode name/value pairs into object properties and values. It accepts one argument as a string that contains name/value pairs and the string will be converted into object properties and associated values.
Name/value pairs look like the following:


myvariable=value&myothervariable=anothervalue


This hidden method, will convert the above name/value pairs into two properties 'myvariable' and 'myothervariable', the values of these properties are the associated values in the name/value pair string, so the value of 'myvariable' in the above string is 'value' and the value of 'myothervariable' in the above string is 'anothervalue'. Heres a little code example to show you what this method does:


// create a new instance of the LoadVars object
vat lv:LoadVars = new LoadVars();
// call the method with name/value pair string
lv.decode("name=Jhon&age=22");

// iterate over all the properties in the ‘lv’ object
for(var i in lv){
//trace the property name and property value
trace(i+":"+ lv [i])
}

The output of the above code will be:
name:Jhon
age:22


Conclusion:

The LoadVars Object is the powerful way for simple communication with any server or for loading data from text files. However for complex data communication it is recommended to use WebService Connector component available in Flash MX 2004 Professional or you can also use Flash Remoting MX for most effective and efficient way of communication. Flash Remoting MX uses Action Message Format (AMF), a binary message format designed for the ActionScript object model which was modelled on the Simple Object Access Protocol (SOAP). AMF uses a packet format to relay information so the communication with the server will be efficient.


| 1 | 2 |

Rate This Tutorial :
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
  

Home | About Us | Contact Us | Feedback | Advertise with us

Best viewed in 800x600 resolution with Internet Explorer.

Site Developed and Hosted by  ethicsolutions.com
All rights reserved with FlashAdvisor

Flash is a registered trademark of Adobe Systems, Inc.

2 users online