ActionScript.org Flash, Flex and ActionScript Resources - http://www.actionscript.org/resources
Write to File: Flash to Server Side interaction
http://www.actionscript.org/resources/articles/580/1/Write-to-File-Flash-to-Server-Side-interaction/Page1.html
Josh
Just a flash developer trying to help out.
 
By Josh
Published on March 31, 2007
 
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.

Write to File: Actionscript

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:

[as] stop(); 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. else { myVars.userName = name_txt.text; myVars.userEmail = email_txt.text; myVars.sendAndLoad("writeToFile."+serverLang, myVars, "POST"); } }; [/as]
 

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.

Write to File: PHP
[php] [email protected]>; $add = $userName . "<" . $userEmail . ">;"; //opens or creates (if it doesn't already exist) our text file (emails.txt) //for writing (not reading) and places the pointer at the end. $open = fopen('emails.txt', 'a'); //writes to our specified file our string $write = fwrite($open, $add); //sends a response back to flash stating success or failure //since fwrite returns a value of true or false, $write will be either true or false if($write) { echo "&verify=success&"; //prints to screen &verify=success which flash will read and store as myVars.verify } else { echo "&verify=fail&"; //prints to screen &verify=fail which flash will read and store as myVars.verify } //closes the PHP file. ?> [/php]

The next page will have the asp code.

Write to File: ASP
[php] <% 'decalare needed variables Dim fso, file, name, email, i 'Get data from variables coming from flash name = Request("userName") email = Request("userEmail") 'sets variable fso equal to a file system object set fso = Server.CreateObject("Scripting.FileSystemObject") On Error Resume Next 'Check to see if the text file already exists if fs.FileExists("emails.txt") = true then On Error Resume Next 'If file already exists then open the text file for editing Set File = fso.OpenTextFile(Server.MapPath("emails.txt"), 8, 0) On Error Resume Next 'add the variable name to the text file file.writeline name On Error Resume Next 'add variable email to the next line of the text file file.writeline email On Error Resume Next 'add a blank line to text file. Number indicates number of blank lines to add. file.writeblanklines(1) On Error Resume Next 'close object file.close On Error Resume Next else 'if file does NOT exist, create it Set file = fso.CreateTextFile(FlastText.txt, true) On Error Resume Next 'add the variable name to the text file file.writeline name On Error Resume Next 'add variable email to the next line of the text file file.writeline email On Error Resume Next 'add a blank line to text file. Number indicates number of blank lines to add. file.writeblanklines(1) On Error Resume Next 'close object file.close On Error Resume Next end if 'If any errors were encounter then run this code If Err.Number <> 0 then 'tells flash ASP failed and terminates the ASP file. Response.Write "&server_mes=fail" Response.End else 'Send message back to flash saying everything was ok. Response.Write "&server_mes=success" End if 'set fso object equal to nothing set fso = nothing %> [/php] That is it! I hope you enjoyed the tutorial and learned something. If you need some help, just post in the forums here. If you need professional help, feel free to contact me.

BYE!