Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Design > Adobe Creative Suite

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Using the LoadVars Object to Send Data

Now that you have an idea of what data is expected in the database, how do you get it there from the Flash movie? You can use the LoadVars object, new to Flash MX, to accomplish all of your data transfers.

  1. If you haven't already, open cardsender.fla. Click on the frame labeled send, which is where information is collected and sent to an ASP page to be written to the database.

  2. Select the Send button and open the Actions panel. In addition to some error-checking code, which will be discussed in a bit, you'll see the code shown in the figure.

  3. Figure 10.7. Creating a new LoadVars object.

    This creates a new LoadVars object, whose function is to pass data to and receive data from your ASP scripts. Variables to be passed to the script are passed as properties of the object (toName, toEmail, fromName, fromEmail, message, and cardID).


You'll notice that the same names are used for variables, lvo properties, and Access field names. This was done for consistency but is not necessary. The variable names passed to ASP will be whatever property names are assigned to lvo, and they might be stored in the database with completely different field names if desired.

After the LoadVars object lvo has been created, it can be used for multiple transfers of data. Refer to the figure to see which parts of cardsender fill in which properties of lvo and which properties are set by the ASP scripts.

Figure 10.8. Properties of the LoadVars object, lvo, are used to pass variable values back and forth between the Flash movie and ASP scripts. Arrows indicate properties (variables) being passed to ASP scripts.

Because the variables passed to and from the LoadVars object are just properties of the object, they remain in place until deleted. This means that once they have been set as properties (as in the previous code), they can be sent to any number of ASP scripts simply by using any of the following data-transfer methods available with the LoadVars object (here called lvo):

  • send. Properties of lvo are sent to ASP (or another server-side script) as variables via either HTTP GET or HTTP POST, as specified in the send parameters.

  • sendAndLoad. Properties of lvo are sent to ASP and variables are sent from ASP (via response.write statements or other server-side print statements) and are received as additional properties of lvo.

  • load. Variables sent from ASP (via response.write) are received as properties of lvo.

In the final version of the movie, you use the sendAndLoad method to transfer data, as shown in this data-flow diagram. When debugging a program, however, you always start with a simple send (with all response.write going to the browser window instead of back to Flash) to make sure variables are being received by the ASP script as you expect.

Figure 10.9. Displaying the content of your query with variables passed from Macromedia Flash in the browser window.

For any application in which Flash and the server are sending data back and forth, it's a good idea to dump the data being sent to a browser window to see if it's being sent correctly before trying to do anything with it in your server-side script.

  1. Using Homesite or another editor, open writetodb.asp, the file that writes a card record to the database and sends back the msgID associated with that card.

  2. Save it as testwritetodb.asp. Delete all lines except lines 5 through 7, and uncomment line 7. You should end up with the code as shown at right:

  3. <%
     insertStr = "INSERT INTO cardInfo (fromName, fromEmail, toName, 
    toEmail, message, cardID) VALUES ('" & request.querystring("fromName") & 
    "', '" & request.querystring("fromEmail") & "', '" & 
    request.querystring("toName") & "', '" & request.querystring("toEmail") 
    & "', '" & request.querystring("message") & "', '" & 
    request.querystring("cardID") & "')"
     response.Write insertStr

    Code Listing 6: If Sending your query to the browser before using it enables you to first make sure your variables are being passed correctly from Flash to the ASP file.

  4. Save testwritetodb.asp.

  5. This causes your insert string to be written to the browser. You'll need to change cardsender temporarily to call this file.

  6. Open cardsender.fla. Click frame 2, select the Send button, and open the Actions panel. Change the line lvo.sendAndLoad("writetodb.asp", lvo, "GET"); to the code shown:

  7. lvo.send("testwritetodb.asp", lvo, "GET");
  8. Save the movie as testcardsender.fla and publish it to produce testcardsender.swf and testcardsender.html. Open the .html file in a browser, with the URL http://localhost/ <directory-within-wwwroot>/ testcardsender.html. Click Send on the opening page, fill out the form, and click Send on the form page.

  9. You should see a new browser window open with a query that includes the values you typed in and sent to the ASP.

    Figure 10.10. Displaying the return string in the browser.

    After you're sure the variables are being sent correctly (either via GET/request.querystring as used here or via POST/request.form), it's a good idea to make sure the variables are also being returned as you expect. You can do this by displaying your output string to the browser, using the send command (which sends data only one way—out of Flash).When the correct output string is displayed in the browser, you can then use the more powerful sendAndLoad, which returns the data "invisibly" to Flash.

  10. To make this test, change the line in Step 3 to:

  11. lvo.send("writetodb.asp", lvo, "GET");

    Code Listing 7: Sending the ASP output to the browser, using the send method, enables you to ensure that the return string is being generated correctly in the ASP file. You can then substitute sendAndLoad to have the output returned to Macromedia Flash.

  12. Save and publish testcardsender.fla. If you open testcardsender.html in the browser, as above, you should see a new window open with output, as well as a new entry in cardInfo in greetings.mdb.

  13. fieldError = 0;
     requiredFields = ["fromName", "fromEmail", "toEmail"];
     for (i=0; i<requiredFields.length; i++) {
      if (this[requiredFields[i]] == "" || 
        this[requiredFields[i]] == null) {

    Code Listing 8: Code on the Send button (in the send frame) enables you to make sure entries have been made for sender name and email and recipient email before attempting to send.

  14. If not, you'll need to fix whatever is causing the problem. This could be an incorrect DSN entry, the location of the .mdb file, or the properties of the .mdb file. A common problem, for example, is failing to check Write Allowed in the Everyone setting of the .mdb file's properties. This produces an error message stating that "Operation must use an updateable query."

In this project, you check for errors (blank required fields) from within the code on the Send button in the frame labeled send before sending any data to ASP. More complex error checking (such as string manipulations that involve checking for a valid email address) might be better handled on the server side, with an appropriate code being sent back to Flash to indicate any errors.

  • + Share This
  • 🔖 Save To Your Account