Difficulty Level: Intermediate

Requirements: Flash 7+

Topics Covered: LoadVars,

Assumed Knowledge: Basic event handling

Sources: see bottom of page

Write to File (Flash 7+)


In this small tutorial, I’m going to walk you through how to send data from Flash to a server side language which will in turn write that data to a text file. So let’s get started. Get the sources provided. Open the fla.


Here is what you should see:

var serverLang:String = "php";
/* For this tutorial the serverLang variable will hold which server-side file you wish to use.
   For ASP, set serverLang = "asp", for PHP, set serverLang = "php"
   This feature is only for convience.

var myVars:LoadVars = new LoadVars();
myVars.onLoad = function():Void
 if (myVars.verify == "success") status_txt.text = "data Saved";
 else                            status_txt.text = "Failed Save";
submit_btn.onRelease = function():Void
 //Here we are validating the data. This insures the email address contains
 //both the "@" and ".", If not, it stops the script and alerts the user.
 if (!email_txt.length || email_txt.indexOf("@") == -1 || email_txt.indexOf(".") == -1)
  status_txt.text = "Invalid Email.";
 //This validates the subject line contains text
 else if (!name_txt.length)
  status_txt.text = "Missing Name";
 // if the script has gotten here, then the input is valid
 // submit the input to the server side script.
  myVars.userName = name_txt.text;
  myVars.userEmail = email_txt.text;
  myVars.sendAndLoad("writeToFile."+serverLang, myVars, "POST");


On line 2, there is a variable called serverLang. Set this variable to either “php” or “asp” depending which server side script you will be using.


Line 9 creates our LoadVars object. I call it “myVars” (yes how intuitive, isn’t is?) and give it a data type of LoadVars. The format of making variables looks like this:

var myVariableName:DataType = new DataType( );


Line 10 handles the response we get back from the server once we submit the data. Notice Flash does not care what the server side language it is. It could be php, asp, perl, etc and Flash treats it all the same way—pretty neat for us. The server side script will respond back with a variable called “verify”. From the script, “verify” will either be “success” or “fail”. We could have made it respond back with “Fred” and “George”; it does not matter so long as we know what to expect back in Flash. But let’s stick with common practice for now. So myVars.verify will either be “success” or “fail”. Line 12 checks for the equality of myVars.verify to “success”. If the statement is true, we display the string “data Saved” in our text field. Notice since there is only 1 command after our "if" statement that we do not have to use the braces { } after the "if". You are more than welcome to use them should you like. However, I think the script is cleaner without them. Should the "if" statement fail, we put an error message in the text field.


Inside the onRelease command we want to send the data to our script, but first we need to check to make sure the data is valid before sending it. Line 20 checks the presence of an “@” symbol and a “.” in the email field. Since all valid emails contain both of these, checking for those is a good idea. Line 24 checks to make sure the name field has any length to it. If all of the information checks out, we add the data to the LoadVars object we created on line 9 and then send the data to the server side script with line 33 which takes the format as follows:

loadVarsObject.sendAndLoad(“url”, loadVarsObject, “METHOD”);

where METHOD is either “POST” or “GET”.


Continue on to the next pages to see the scripts. The description of what is happen is in the comments above each line. Please read them carefully. The asp script is provided courteous of Chad “Cota” Workman.