AS2 → AS3: LoadVars AS3 Equivalent

Download Example Files

ActionScript 3 has done away with the LoadVars class and when I was updating my ContactForm for AS3 I was trying to figure out how to mimic the sendAndLoad() method that LoadVars provided. I stumbled upon an article by Peter Elst which explained how to do this so I'm going to outline the differences here.

Let's take a look at the old AS2 code:

Actionscript:
  1. var msg:LoadVars = new LoadVars();
  2. var msgSent:LoadVars = new LoadVars();
  3.  
  4. msg.var1 = "one";
  5. msg.var2 = "two";
  6.        
  7. msgSent.onLoad = function($success:Boolean):Void
  8. {
  9.     if ($success)
  10.     {
  11.         trace("Message sent.");
  12.     }
  13.     else
  14.     {
  15.         trace("Message failed.");
  16.     }
  17. };
  18.  
  19. msg.sendAndLoad("http://www.reintroducing.com/script.php", msgSent);

And here is the AS3 equivalent:

Actionscript:
  1. var scriptRequest:URLRequest = new URLRequest("http://www.reintroducing.com/script.php");
  2. var scriptLoader:URLLoader = new URLLoader();
  3. var scriptVars:URLVariables = new URLVariables();
  4.  
  5. scriptLoader.addEventListener(Event.COMPLETE, handleLoadSuccessful);
  6. scriptLoader.addEventListener(IOErrorEvent.IO_ERROR, handleLoadError);
  7.  
  8. scriptVars.var1 = "one";
  9. scriptVars.var2 = "two";
  10.    
  11. scriptRequest.method = URLRequestMethod.POST;
  12. scriptRequest.data = scriptVars;
  13.  
  14. scriptLoader.load(scriptRequest);
  15.  
  16. function handleLoadSuccessful($evt:Event):void
  17. {
  18.     trace("Message sent.");
  19. }
  20.  
  21. function handleLoadError($evt:IOErrorEvent):void
  22. {
  23.     trace("Message failed.");
  24. }

As you can see, there is a new URLVariables class that stores the information you want to pass to your script. You then pass that URLVariables instance to the URLRequest's data property. Essentially, what this does, is create a query string with all the variables appended to it. The above actually looks like this when it is sent to the server:

http://www.reintroducing.com/script.php?var1=one&var2=two

That is what the PHP script receives and then handles it on the script's end to do whatever it is that you are wanting to do. I also explicitly set the URLRequest's method to be POST by setting the URLRequestMethod.POST constant (for GET, you'd just set URLRequestMethod.GET).

If you found this post useful, please consider leaving a comment, subscribing to the feed, or making a small donation.

50 Comments

thanks a lot! useful.

[...] that sometimes what used to be accomplished in few steps now takes more. One of these is the old AS2 LoadVars object. It was pretty straight forward, but now it requires multiple classes and methods to make work. [...]

Thanks for the concise summary, really helpful.

No problem Tim, glad it helped!

Thanx for this!
very helpful,
like other stuff on your site,
so,
thanx for that too 🙂

Maciej,

Can you read query string parameters with this URLVariables class. For instance in your example:

scriptVars.var1 = "one";
scriptVars.var2 = "two";

Say in the URL there was a querystring parameter that already exists http://www.dkdkdkdkd.com?var1=one

Can I do var strQueryStringValue:String = scriptVars.var1 and it store the value "one" in the string variable?

Richie,
Unfortunately it's not as easy as that. There has to be a javascript call to get the URL and then parse the URL in Flash. I'm gonna work on making a class to do this in AS3.

Thanks! Helped a lot!

Exactly what I was looking for. Thanks for taking time to post little snippets like this...

glad it helps guys.

Thanks so much, this AS2 -> AS3 thing is doing my head in!

Thank you for just being you! You're the man, and I take back all the bad things I said about you. Ok, so maybe not all of them.

Seriously though, thanks man, this actually was a nice find and hooked up my problem nicely. Now that we can finally use AS3 (almost 3 years after it came out...go figure), it's been a serious headache trying to handle some of the simplest things that are now different. I feel like a n00b again....it's awesome!

haha, you're welcome jason, although the game i just did for you guys was still in AS2 🙁 glad this helped.

This is exactly what I was looking for. Having to re-write a bunch of tracking code into AS3. Feel like I'm back at square one again.... until I came across your wee gem!

glad I could help, Patrick!

Matt, have a question for you:
My Flash is embedded in a .ASP page that receives a query string as part of the URL, decodes it and passes it to the Flash through loadVars. How would I change your code example to reflect this?

Thanks for any and all advice you can give me...

@Patrick: you don't need this for what you are doing. what you need is pulling in FlashVars which you can find examples of (both AS2 and AS3) here: http://evolve.reintroducing.com/2008/03/21/as2-to-as3/as2-%E2%86%92-as3-retrieving-flashvars/

hope that helps!

Man, talk about a life-saver! Thanks!!

Matt,
Do you do consulting? If so, please drop me a note on my email address.

@Patrick: glad i could help 😛 and yes, i e-mailed you back.

Hi I get an error message when I run the full script!

Description:
"1046: Type was not found or was not a compile-time constant: Event.

Source:
"function handleLoadSuccessful($evt:Event):void

But if I leave out the eventlisteners and the functions it works, but then I'm not able to tell if if succeded in flash.

@Alexander Bech:
Don't forget to include:
import flash.events.*;

you have to import the Event class at the top of your script:

import flash.events.Event;

thanks alot! Both of you 😉

I'm new to AS 2 or 3 - but in the AS2 script - any information returned by the PHP file will end up in msgSent - example, if I have a $imageName = "image.jpg", then shouldn't I refer to that back in AS2 script as msgSent.imageName ?

If so - how is that being done in your AS3 version? If it isn't, then how would that be done in AS3

Thanks in advance, and thank you for this script.

HI been using this for a while, I have run into a weird situation where the script works fine in firefox but fails in internet explorer. It only happens with a specific php script I am running. Any ideas what might be causing this?

@Shaun: If its working normally but only fails with a specific PHP script, then I'd have to venture and say that its the PHP scripts fault in which case I can't really help you since I don't know very much PHP :\

Thank you! Very useful!!

thx,useful....

Very useful. Although once again, I am forced to step back from both code examples, and see the beautiful simplicity of AS2, and the confusing "wordiness" of AS3.

How this is an "improvement", I have no idea.

But thank you for the great tip.

Great tip. Thank you !!!

Im try somthing like this :

function handleLoadSuccessful($evt:Event):void
{

Text2.text=scriptRequest.data("Url1")
}

But is not working 🙁

Many thanks, a great help! 🙂

[...] اونو هم برای اکشن ۲ و هم برای اکشن ۳ نوشته و اینم آدرسش : http://evolve.reintroducing.com/2008…s3-equivalent/ و منم با آموزش خودم سعی می کنم دیگه این مسئله رو در این [...]

What about a result? The old AS2 sendAndLoad, LoadVars provided for the prospect of a return from the script with a "result.onLoad" method. How is that handled with AS3?

@John: The Event.COMPLETE acts the same as the onLoad method in AS2 did and you can pull a data value in the event parameter.

hi
can i know how to get variables from an external php page

Thank you very much, very very usefull

If anyone is having problems working out how to get variables back from this method, try the following.

Insert this line before the .load

scriptLoader.dataFormat = URLLoaderDataFormat.VARIABLES;

This tell it that we are expecting url encoded variables back

Then in the handleLoadSuccessful function you can access the variableslike this:

scriptLoader.data.returnVar1

Where returnVar1 is an example variable name.

More info on the flash help pages
http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/net/URLLoader.html

Hi Matt,

Going back to your comment dated July 3, 2008 in response to Richay, did you ever get that class built and might you have a full sample including the required javascript?

Thanks for everything.

@Keith: I do have a class to do that now and can send it to you if you can provide your e-mail. It relies on some other classes to work so I'll have to package it all up for you.

Hi Guys,

I'm verry happy I came across this page.

I've been trying to implement your script to the button on my contact form page and unfortunately I can't get it to work.

Here's my code I have placed on the timeline of my fla:

var scriptRequest:URLRequest = new URLRequest("php/email.php");
var scriptLoader:URLLoader = new URLLoader();
var scriptVars:URLVariables = new URLVariables();

Submit.addEventListener(MouseEvent.CLICK, submitForm);
//Submit is my button
function submitForm(event:MouseEvent):void {

scriptLoader.addEventListener(Event.COMPLETE, scriptOk);
scriptLoader.addEventListener(IOErrorEvent.IO_ERROR, scriptError);

scriptVars.theName = contactName.text;
scriptRequest.method = URLRequestMethod.POST;
scriptRequest.data = scriptVars;
scriptLoader.load(scriptRequest);
}

function scriptOk($evt:Event):void {
MovieClip(parent).gotoAndPlay(2);
}

function scriptError($evt:IOErrorEvent):void {
MovieClip(parent).gotoAndPlay(2);
}

Can somebody advise why it's not working?

Thanks

Well sorry...

I should be more specific...

The script is working, it sends email however in my php file where i use $theName I can't see any data...
It looks like the above script doesn't send the data to php file.

Thanks!

@zee: what does your PHP script look like?

Matt, thx for your reply. I have figured that out. There was a silly error with paths to objects. Thanks

Excelent e Funciona Perfecto, aca les dejo como pasar variables al insert, etcc.... http://msdn.microsoft.com/en-us/library/cc793139%28v=sql.90%29.aspx

thanks i am curently working on AS2

making my transition from as2 to as3 and this was really helpful - but only with the comment of John Crickett... Also, a lack of a predisposition of knowing to add in import flash.net.* before hand too... But thank you! Still helpful!

Many tanks bro. But how do I use this with asp. I'm not good with php. Trying to link up my adobe AIR program with an external database like access. How do I go about the code to link my AIR program with the asp file to query the database and have the returnd value displayed in the program. Many tanks once again

Leave a comment

(required)

(required)