Sending Flash Data to ASPX or PHP files

Jun
17

Posted by Anthony in Flash, Tutorials, Web Design tagged , , , , ,

If you are working on any Flash website projects that need to pass data out to an ASPX or PHP file (or any other type of file requiring to send data via POST or GET), you can use the code below.  Note that this code is written in ActionScript 3.

/* --- Begin Code --- */
var firstName:String = "Anthony";

function submitForm(e:MouseEvent):void {
   var varsToSend:URLVariables = new URLVariables();
   varsToSend.firstName = firstName;
   // Insert additional values to send

   // Create URL Request, set to POST, and attach data
   var formRequest:URLRequest = new URLRequest("flash.php");
   formRequest.method = URLRequestMethod.POST;
   formRequest.data = varsToSend;

   // Create URL Loader, attach listeners, and load URL Request
   var varLoader:URLLoader = new URLLoader;
   varLoader.addEventListener(Event.COMPLETE, onLoaded);
   varLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
   varLoader.load(formRequest);
}

function onLoaded(e:Event):void {
   // Insert success actions here
}

function ioErrorHandler(e:IOErrorEvent):void {
   // Insert error actions here
}
/* --- End Code --- */

A few notes on the code:

  • The submitForm function in this example is called on a MouseEvent.CLICK event (noted with the e:MouseEvent passed into the function).
  • If you need to send via GET, change the URLRequestMethod to URLRequestMethod.GET.
  • One thing I noticed about the URLRequest was that on the particular domain I was working on, using an absolute URL gave me some bugs.  If I used http://www.myfakedomain.com/flash.aspx, my tests failed on PCs but worked on Macs.  If I removed the “www”, resulting in http://myfakedomain.com/flash.aspx, my tests failed on Macs, but worked on PCs. I don’t know if this was an issue with how the domain was set up (since I only worked on the Flash portion, not setting up the domain) or if it is a Flash URLRequest bug.  Either way, I found that using a relative URL (“flash.aspx”) worked on Macs and PCs, and didn’t throw me any errors.

Hope this sheds some insight on how to send data out of Flash.  Happy coding!

2 Comments for this post:

  • Patrick says:

    Hey Anthony, I am new to server side scripting within flash and I just wanted to know if you could share how the .aspx file code would look to receive the data from the Flash Form.

    From some other tuts on the net that I have been looking at, the submit button on the form would also need to be passed into the actionscript… sorry for the ignorance but how do you handle that with your example above?

    Any help would be great as Im finding it hard to find Flash Forms/ASPX examples around..

    Cheers!

  • anthony says:

    Patrick,

    Admittedly, I don’t normally do much of the ASPX/PHP side of things. I usually just set up the Flash file to send out the data, and let the developers I work with deal with accepting the data.

    With that said, I do have a little insight into how to get the values in both an ASPX page and a PHP page:

    ASPX
    Create a variable that accepts form values, such as:
    string FirstName = this.Request.Form[0];
    (In the line of code above, “this” points to the ASPX file itself, “Request” is the type of action you want, and Form[0] gets the first value from the form. Additional numbers in Form[] will give you the other values passed, if applicable.)

    PHP
    $name = $_GET['firstName'];
    (In the line of PHP code above, ‘firstName’ can be any variable passed out of Flash, but the value in the brackets must match what the Flash file sends out.)

    Hope it helps!

Leave a Comment