Flash and ASP IntegrationBY
Dan Waters
ASP AND flash? How?
Macromedia's Flash and Microsoft's Active Server Pages technologies are two products with an extremely heavy impact on the world of web site development. Flash allows you to create zippy, vector-based animation and interactivity in a small ActiveX control, and ASP allows you to create dynamic Html Content on the fly. Hopefully, by the time you're done skimming this article, you'll be confident enough to use asp in the creation of zippy, Vector-based Flash Content on the fly.
To Many Developers, A Site Done in Flash IS Worth Many Done in Html. The Ease of Use and Graphical Appeal of Flash IS Unmatched. Where Many of US Get Stuck, However, Is At the Question "How am i Going to Get Data Into Flash from an external source? "The answer to this question is also the key to a matcheningly success
The Concept
The first thing you need to know about Flash is the method in which it handles variables from remote files. Flash will take an URL-encoded query string and transform it into a list of variables within its own memory. An example of a string that Flash Recognizes as Variable Definitions Looks Like this:
SIZE = Medium & Color = Navy Blue & Style = Mandarin Collar
WHEN You Import this String Into Flash, You Will Have Three New Variables: size, color, and style. These Variables Will ALREADY BE INITIALIZED; LOADed with the data you passed in the string.
Sizemediumcolornavy BluestyleMandarin Collar
Be aware that Flash is incredibly easy to please as long as you employ the Server.URLEncode method. Flash does all the decoding itself. In the same respect, when Flash sends variables to a script, they will already be URL-encoded.Techniques
Returning a Valid String from ASP
The Code Below Will Return a Valid String To The Flash Movie.
<% @ Language = "VBScript"%>
<%
Option expedition
Dim Var (3), I, COUNT
i = 0
Count = 3
Var (0) = "Winken"
Var (1) = "blinken"
VAR (2) = "NOD"
Do While i Response.write "Var" & I & "=" & Var (i) & "&" i = i 1 Loop Response.write "i =" & i %> WHEN You Execute this Program, ASP RETURns this string: Var0 = winken & var1 = blinken & var2 = NOD & I = 3 Passing Values from Asp to Flash Remember That Query String I MENTIONED ABOVE? You'll Be Using this Format with asp next. To Pass Variables from Asp To Flash, You Must Write a String Back to the Response In The Above Format. To load variables from an asp file, you shouth users Load Variables ("MyScript.asp", 0) (The load variables command can be found by choad / unload morse from the dropdown list) Generate HTML IN A New page. The zero in the above script represents the level at which you will load the movie. If you have movies stacked upon one another, you should reference them by their z-order. _Level0 is the first movie, _level1 is the movie stacked above the first , And so on. for out, we'll assume there is online, one movie in the picture and use 0 for the level specification.passing value from flash to asp Surely you willow send out form data from flash to ask the as of send variables from flash to an asp program, use the folowing actionscript code: Load Variables ("MyScript.asp", 0, Vars = POST) WHEN You Send Variables Using Post, You CAN Access Them in Your ASP Script Using One of Two Methods: Request.form ("Flash_Var_1") Request ("Flash_Var_1") That's Right ... When You Post Variables, Flash Sends Them in The Form Collection. WHEN You send Flash Variables Using GET, HOWEVER, THEY Are Written INTO The Query String, And Subsequently You Should Use Request.QueryString ("Flash_Var_1"). When you specify "Do not send" as a destination for the variables, Flash assumes you're loading data from the ASP file. You can use a query string parameter for the URL value of the Load Variables call and still retain the "don 'T Send "Variable Transmission Specification To Avoid Overwriting Variable VALUES. Exercise: Transferring Valuesfrom Flash To Asp To A Database To Asp To Flash Download Support Materials for EXERCISE 1 from here. View the code here. The Files Are Named As Such: Employee.mdbaccess databaseemploye.aspasp scriptemployee.flaflash fileemployee.swfflash movieemploye.htmlmovie mounting Page We will be employing ADO database routines in this example to make all this useful to you in the future. I have created a table called Employees in a file called employee.mdb which resides in the same directory as the script. The table looks like the One Below: idNamelastNamefirstPosition1Smithjohnceo2BrownsusanMarketing Director3stanfordrobertsales Reperestative That's it; three simple records Notice how all the data is in capital letters:. In a real-world application, this would really only be relevant to the fields upon which you would search Putting the data in capital (or lowercase) letters guards. Against Case Mismatch In Searches, Because We Can Ucase The Data from Flash Before Sending It To ADO. Save Your Table As Employees and Save Your Database As Employee.mdb in a Directory by Itself. Next, We'll Make The Input Screen in Flash. Open Up Flash or Use The Support Materials Available. The Data Entry Frame In the first frame, create a screen with a text label that reads ". Please enter employee's last name" Next to it, create a text field (push the | ab button down to make it an editable field) and right-click on it . Go to Properties, and where it says Variable, enter nameLast. you should go to the first frame's Properties and add an action script that simply says "Stop." This prevents the movie from advancing until you have entered a value and clicked Submit. The Submit ButtonCreate a Button That Says Submit. The Drop It Onto The Stage and Go To ITS Instance Properties by Double Clicking It. In The ActionS Tab, Enter This Action Script: Go to and play (2) THIS Sends the user to the second frame. Loading the variables behind the scene Create, In The Second Frame, A Blank KeyFrame with One Frame Action: loading variables ("Employee.asp", 0, Vars = GET) The "Loading Data" loop In The Third Frame, You Want to Create A Short Animation Tells The User That AS Loading Data. In this case IF (Position Ne ") Go to and stop (9) END IF This checks to make sure that ADO has pulled our last variable, Position, from the database and returned it to the response Otherwise it continues to play the Loading sequence until frame 5, at which we have this action.: Go to and play (3) This Causes The Animation To Check The Value of Position. When AS Finished, The Animation Will Automatically Go To Frame 6. The Result Frame At frame 6, we should have a small text label that reads "ASP has returned the following results." Below this text label, we should have three more text labels in a row which read "Last Name", "First Name", and "Position" These are captions for our editable text fields below Insert editable text fields... (click the | ab button on the toolbar when you drop it, just like in the first frame) Set one's Variable equal to "nameLast", set Another One's equal to "namefirst", and the last one's equal to "position". Publishing Save Your Document File As Employee.fla in The Same Directory As Your Database. THEN, Go To File Menu -> Publish and You Will Find Employee.swf and Employee.html Residing In The Same Directory You Saved Employee.fla. Okay, That's it, you're done with the flash side of it. Now it's time to move on to ask.Open Up your favorite asp Editor (Mine IS textpad!), Such As Interdev or even notepad. You will be useful ADODB recordset and connection with a SQL connection string. you will not return anything to the response except the URL-encoded variable definition string. Are you ready? Well, get that way, because here's the code. <% @ Language = "VBScript"%> <% Option Explicit 'Don't Ever Let Me Catch You without this line! DIM ORS, OCONN 'RecordSet and connection Objects Set Oconn = Server.createObject ("AdoDb.Connection") Oconn.connectionstring = "driver = {Microsoft Access Driver (* .mdb)};" & _ "DBQ =" & Server.mappath ("Employees.mdb") Oconn.open SET ORS = Server.createObject ("AdoDb.Recordset") Ors.open "Select * from Employees", Oconn, 2, 3 'The next line looks for the specified name and ucases the 'Last Name We Searched for To Avoid Case-Sensitive Issues. Ors.Find "Namelast = '" & ucase (Request.QueryString ("Namelast") & "'" 'IF the last name does not exist, the return not found to the response. 'OtherWise Return Appropriate Variables. IF Ors.eof Then Response.write "Namelast = NOT FOUND & NAMEFIRST = NOT FOUND" & _ "& Position = NOT FOUND" Else Response.write "namefirst =" & server.urlencode (ORS ("Namefirst") & _ "& Namelast =" & Server.urlencode (ORS ("Namelast") &_ "& Position =" & Server.urlencode (ORS ("position")) Endiff 'Clean Up and say goodbye. Ors.close SET ORS = Nothing Oconn.close Set Oconn = Nothing %> Save this code as equployee.asp Now, access employee.html through the local intranet You should, if you have not already, stick all these files somewhere in your web root and access it via http:. //Localhost/employee/employee.html (or wherever you have Stored the files). Since asp is processed Server-Side, you cannot Access Employee.html Via The Hard Drive and Expect Values from the ASP Application. This Means you will have an endless "loading" loop Once you have pulled up the page on the Intranet, enter a name which you know is in the database, such as Smith. ASP will return the values to Flash, and you will see the results when ADO is done processing. Good luck with this! If you have trouplas, please contact me at dan@catapultic.com.