Categories
Featured jobs
» More ActionScript, Flash and Flex jobs.
» Advertise a job for free
Our network
Advertisement

 »  Home  »  Tutorials  »  Flash  »  Beginner  »  Write to File: Flash to Server Side interaction

Write to File: Flash to Server Side interaction

By Joshua Musselwhite | Published 03/31/2007 | Beginner | Rating:
Joshua Musselwhite
Skills: Actionscript 1-3, PHP, mySQL, XML, HTML, CSS, Javascript
Portfolio
Availability: Always
 

View all articles by Joshua Musselwhite
Write to File: Actionscript

Written By: Joshua Musselwhite

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:

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");
 }
};

 

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.

Spread The Word / Bookmark this content

Clesto Digg it! Reddit Furl del.icio.us Spurl Yahoo!

Comments
  • Comment #1 (Posted by Damon Schreiber - damons at mac.com)
    Rating
    Great tutorial which helped me a lot! One thing I had to change was
    if (!email_txt.length || email_txt.indexOf("@")
    I changed to
    if (!email_tx.textt.length || email_txt.text.indexOf("@")

    And then it worked great. Many thanks,!
     
  • Comment #2 (Posted by le - datic)
    Rating
    nice and simple code. easy to understand. good tutorial for beginners. just add a line feed at the end so they are neatly written in each line.

    Thanks
     
  • Comment #3 (Posted by Emilia - e.bakala at gmail.com)
    Rating
    hi, i am the beginner and being the honest it's not really clear for me.

    Although ActionScript code is very clear, the same as writeToFile.php file's content, it's still not said how can you test the project, i mean where exactly to put the files from your tutorial.
    i placed the files on server and it didnt work, it didn't work on localhost as well. do i need any special software for testing it ?

    i think the hardest part for the beginners is not the code understanding (as it's very clear), but informations about how to test the files .

    I would be very grateful if you can guide me somehow or recommend some good book. thank you :)
     
  • Comment #4 (Posted by bee - cat at yours.com)
    Rating
    Hi,

    I think I am having the same problem. I tried putting that under my applications folder or desktop. It keeps saying save failed. I put some debug line like msgbox in the asp page but nothing being called. I am a very beginner here (I have experience in c++ and vb)...I am kind of confused.
     
  • Comment #5 (Posted by mindlesswizard - psychodenied at hotmail.com)
    Rating
    ::- Bingo -::
    i got what i searched for !!!!will be seeking for ur help !!! stay TUNNED !!!
    cheers
     
  • Comment #6 (Posted by somebody)
    Rating
    looks good, but I can't find the source file!
    Where is it?
     
  • Comment #7 (Posted by Ryanni - ryanniride at hotmail.com)
    Rating
    yeah, this looks as if its just what I've been searching for. But I also cannot find the source. Guess I'll have to work around it.
     
Submit Comment



Search Entire Site
Add to Google
Advertisements
Article Options
Latest New Articles
Set up a simple IIS Server for Flash
by Peter McBride

Day 1 at FITC Toronto 2008
by Anthony Pace

Simple reflection effect with AS2
by Jean André Mas

ActionScript.org Meets Josh Tynjala (aka dr_zeus)
by ActionScript.org Staff

Rapidly Create Online Flash Movies to Help Users Market, Sell and Support Software and Hardware
by Sabrina F

mailing list
Enter your email address:
mailing list
Subscribe Unsubscribe
© 2000-2007 actionscript.org! All Rights Reserved.
Read our Privacy Statement and Terms of Use...
Our dedicated server is hosted and managed by WebScorpion Webhosting.