Previous Page
Next Page

Recipe 19.2. Loading Variables from a Server-Side Script

Problem

You want to load variables into a Flash movie from a server-side script (ColdFusion, Perl, PHP, etc.).

Solution

Use the URLLoader.load( ) method in conjunction with DataFormat.VARIABLES as the dataFormat to read URL-encoded data generated by a server-side script into a Flash movie.

Discussion

The ActionScript to load variables from a server-side script is exactly the same as that used to load variables from a text file. When you call the load( ) method, you should pass it the URL of the script from which you want to load the variables, and then handle the results by listening for the complete event as shown in Recipe 19.1.

When the values for variables are generated from a database or another resource accessible only to the server, use server-side scripts as the source for variables loaded into the Flash Player. The script that you use must output URL-encoded data only, beginning from the first character. If you are writing a CGI script in Perl, the result is URL-encoded, so you won't need to make any special adjustments; for example:

#!/usr/bin/perl
     
# In a more practical example this value would be retrieved 
# from a database or other server-side resource.
$someText = "test";
     
# Define the Content-Type header.
print "Content-Type: text/plain\n\n";
     
# Output the name-value pair.
print "someText=$someText";

However, when you use a ColdFusion page to generate variables to load into your movie, you need to take steps to ensure that it outputs URL-encoded data and that the output begins from the first character. Otherwise, extra whitespace characters may precede the output of the URL-encoded data, causing improperly decoded values. Here is what you should do:

  1. Include the <cfsetting enablecfoutputonly="yes"> tag at the beginning of the document.

  2. Make sure to enclose any values you want to output within a <cfoutput> element.

  3. Place the whole document in a <cfprocessingdirective suppresswhitespace="yes"> tag. This ensures that no extra whitespace characters are output.

For example:

<cfsetting enablecfoutputonly="yes">
<cfprocessingdirective suppresswhitespace="yes">
  <cfset someText = "test">
  <cfoutput>someText=#someText#</cfoutput>
</cfprocessingdirective>

If you use PHP, perform output using echo or print from within a processing directive, such as:

<?php
$someText = "test";
echo "someText=$someText";
?>

Other preprocessor markup languages may or may not require additional steps to ensure proper output. JSP and ASP (.NET and classic) do not require any special considerations. One trick that seems to work in most cases is to simply surround each name/value pair with ampersands. This ensures that the variable delimiter is present so the variables can be correctly decoded by the Flash Player. This solution should work regardless of the language/platform you are using (ASP, JSP, CFML, etc.). For example, if you don't want to use the <cfprocessingdirective> and <cfsetting> tags in your ColdFusion page, you should be able to rewrite the preceding ColdFusion example as follows (note the ampersands in bold):

<cfset someText="test">
<cfoutput>&someText=#someText#&</cfoutput>

In each of the preceding examples, the server-side script returns a single variable, someText, to the Flash Player. The following example uses ActionScript code to load the variable from a script and display it in the movie:

package {

  import flash.events.*;
  import flash.net.*;
  import flash.util.trace;

  public class Example(  ) {

    public function Example(  ) {
      // Create the URLLoader instance to be able to load data
      var loader:URLLoader = new URLLoader(  );

      // Define the event handler to be invoked when the load completes
      loader.addEventListener( Event.COMPLETE, handleComplete );

      // Configure the loader to load URL-encoded variables
      loader.dataFormat = DataFormat.VARIABLES;

      // Attempt to load some data
      loader.load( new URLRequest( "getSomeText.cfm" ) );
    }

    private function handleComplete( event:Event ):void {
      // Cast the event target as a URLLoader instance because that is 
      // what generated the event.
      var loader:URLLoader = URLLoader( event.target );
  
      // Access the variable(s) that were loaded by referencing the 
      // variable name off the data property of the URLLoader instance.
      trace( "someText = " + loader.data.someText );
    }

  }
}

Of course, many of the same things apply to loading variables from a script as when loading from a file. For example, if you don't know all the variable names, you can use a for...in statement to list them and their values, as shown in Recipe 19.1.

See Also

Recipe 19.1


Previous Page
Next Page
Converted from CHM to HTML with chm2web Pro 2.85 (unicode)