ASP Flash MX Tutorials


Flash MX  and ASP Communication

Text File
Keyword Search Results



Name:  Survey 2
ID = ASP1   Version:    1   SWF file size =      Posted: 5/10/03
Level:  Intermediate/Advanced                                             
Primary Focus:  LoadVars object, sendAndLoad , ASP
Secondary Focus: Reading and writing text files in ASP
Approach:  Data transfer from Flash MX movie to ASP page, update text file, return
                   updated data to Flash MX movie



The ActionScript code, ASP file, and text file listed below are the software components used in the learning object: Survey2 -Home Computer Use .  



// Survey 2 Home Computer Use
// Author: Bruce Bird
// Minddrops.com


over=false; // when mouse cursor is not over check dots
submissionCount = 0; // allows survey data to be sent
sendData = new LoadVars(); // used to send data to ASP page
returnData = new LoadVars(); // used to receive data from ASP page
totalSubmissions._visible = false; //hide text field
surveyResults._visible = false; // hide text field


// Show hand over radio buttons

function showHand() {
    for(i=1;i<=5;i++) {
        if(_root["c"+i].hitTest(_root._xmouse, _root._ymouse, true)) {
            over = true;
            overNumber = i; // indicates the current check dot the mouse is over
            i=30; // kicks the program out of the for loop
        } else {

    if(over) {
         _root.handCursor._x = _root._xmouse; // handCursor is the movie clip instance
                                                                        //name for the hand cursor image

        _root.handCursor._y = _root._ymouse;
        _root.handCursor._visible = true;
    } else {
        _root.handCursor._visible = false;

// Click choice , show checkmark

function clickChoice() {
    _root["c" + overNumber].onPress = function() {  // c1, c2, ... are the movie clip instance
                                                                                // names of the choice dots

        for(i=1;i<=5;i++) {
            if(i ==overNumber) {
                _root["c" + overNumber].gotoAndStop("Check"); // the choice dots are movie clips
                                                                                        //that have frames labeled Check and noCheck

                   choiceNumber = i
            } else {
                _root["c" + i].gotoAndStop("noCheck");

// Submit and retrieve data to/from ASP file : survey2.asp

function submitIt() {
    submitResults.onRelease = function() { // submitResults is the instance name of the
                                                                // Submit/Results button

        if(submissionCount == 0) {
            sendData.choiceNumber = choiceNumber; // on the left of the equal sign choiceNumber
                                                                                // is a property of the LoadVars object whose
                                                                                // instance name is sendData; on the right choiceNumber
                                                                                // is a variable name

            sendData.sendAndLoad("myFile.asp",returnData, "POST"); // myFile.asp is the name of
                                                                                // the ASP file that will a) receive the data sent from the Flash MX
                                                                                // movie; b) update the survey data stored in the text file, myText.txt,;
                                                                                // c) send the updated data back to the Flash MX movie

            totalSubmissions._visible = true;
            surveyResults._visible = true;
            message = "Data Sent";
            submissionCount = SubmissionCount + 1;  // will prevent the next attempt to submit data
        } else {
            message = " Data Not Sent: You have already submitted your response";

// Define properties of LoadVars instance name returnData

returnData.onLoad = function() {
    for(i=0;i<=5;i++) {
        _root["r" + i] = this["r"+i]; // on the left r1, r2 ..., are text field variables, but on the right
                                                // they are properties of the returnData LoadVars instance




_root.c1.onEnterFrame = function() { // I just picked c1 as one of the movie clips that is always
                                                            //on the stage




ASP  (myFile.asp)

<%@ Language=VBScript %>
<% Option Explicit %>

The date is <%=Date%> and the time is <%=Time%> 


' It is useful to have the above date and time line in your ASP code so that you can check, by clicking
' your browser refresh button, to see if ASP is working on the computer with which
' you are developing your Flash MX and ASP files.

Dim cN, strPath, fso, oFile, tso, cArray(6), i, k

' ++++++++++++ Get the data from the Flash MX movie
cN = Request.Form( "choiceNumber")
Response.Write "The choiceNumber is " & cN &"<BR>"

' +++++++++++++ Transfer data stored in existing text file: myText.txt to variables in this ASP page
Const ForReading=1
strPath = Server.MapPath("myText.txt")
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set oFile = fso.GetFile(strPath)
Set tso = oFile.OpenAsTextStream(ForReading)

Do While Not tso.AtEndOfStream
    cArray(k) = tso.ReadLine ' cArray(0) is the current total number of previous submissions
                                        ' cArray(1) is the current number of first option submissions
                                        ' cArray(2) is the current number of second option submissions, etc 

    k=k +1

Set tso = Nothing
Set oFile = Nothing
Set fso = Nothing

' +++++++++++++++ Update variables

cArray(0) = cArray(0) + 1 ' increase the current total number of submissions by one

Select Case cN      ' cN is the number of the option chosen in this submission
Case 1
    cArray(1) = cArray(1) + 1
Case 2
    cArray(2) = cArray(2) + 1
Case 3
    cArray(3) = cArray(3) + 1
Case 4
    cArray(4) = cArray(4) + 1
Case 5
    cArray(5) = cArray(5) + 1
Case Else
    Response.Write ("Invalid Operation")
End Select

' +++++++++++++++++ Write the updated variables to the existing text file file: myText.txt

Const ForWriting=2
strPath = Server.MapPath("myText.txt")
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set oFile = fso.GetFile(strPath)
Set tso = oFile.OpenAsTextStream(ForWriting)

For i = 0 to 5

Set tso = Nothing
Set oFile = Nothing
Set fso = Nothing

' ****************** Send the updated variables to the Flash movie
For i = 0 to 5
Response.Write "&r" & i & "=" & cArray(i)      ' r0, r1, etc are the properties of the LoadVars
                                                                        ' instance, returnData, in the Flash movie



Text File  (myText.txt)




One hurdle to overcome when developing ASP pages is the initial settings on your computer. After several attempts I was finally able to get ASP to work but this required adjusting settings in my anti-virus program, Windows XP Professional, Windows Explorer, and IIS 5.1.  If you run into similar problems, see  Getting ASP to run on Windows XP Professional.

The ActionScript code is attached to frame 1 of the main time line. Information about the LoadVars class in Flash MX ActionScript can be found in Moock (page 575-589), and Franklin and Makar (pages 342-353).

Useful references for working with text files in ASP  can be found in Mitchell and Atkinson (Chapter 13), Weissinger (Chapter 18), and Francis et al. (Chapter 11)



Francis, Brian et al., Beginning Active Server Pages 2.0, Wrox Press Ltd., 1998

Franklin,Derek and Jobe Makur, Macromedia Flash MX ActionScripting Advanced Training From the Source, Macromedia Press, 2002

Mitchell, Scott and James Atkinson, Teach Yourself Active Server Pages in 21 Days, Sams Publishing, 2000

Moock, Colin, ActionScript for Flash MX - The Definitive Guide, 2nd Edition, O'Reilly, 2003

Weissinger, A. Keyton, ASP in a Nutshell, O'Reilly, 1999

Keyword Search Results

1.  Try the following search words at www.google.com. (Be sure to include the quotation marks.)

                                                                      Pages sited by Google

LoadVars()                                                       3,140

sendAndLoad() LoadVars()                                545

ASP  FileSystemObject                                  32, 700

"reading and writing text files" ASP                       173


2002-7  All rights reserved.  MINDdrops.com