Recipe 18.2 Loading Variables from a Server-Side Script

18.2.1 Problem

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

18.2.2 Solution

Use the LoadVars.load( ) method.

18.2.3 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, then handle the results with an onLoad( ) method, as shown in Recipe 18.1.

You should use server-side scripts as the source for variables loaded into Flash when the values for the variables are generated from a database or another resource accessible only to the server. 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 do not need to make any special adjustments. For example:


# In a more practical example this value would be retrieved 
# from a database or other server-side resource.
$myVar = "test";

# Define the Content-Type header.
print "Content-Type: text/plain\n\n";

# Output the name-value pair.
print "myVar=$myVar";

However, when you use a ColdFusion page to load variables into Flash, 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, in which case Flash cannot properly decode the 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 myVar="test">

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

$myVar = "test";
echo "myVar=$myVar";

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 helps Flash know where to begin and end each variable pair. 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 myVar="test">

In each of the preceding examples, the server-side script returns a single variable, myVar, to Flash. Here is an example of the ActionScript code that loads the variable from a script and displays it in the movie:

// You must first create the LoadVars object.
myLoadVars = new LoadVars(  );

// Then load the variable from the server-side script. This example 
// uses the loadVarsScript.cgi script, which is assumed to be in 
// the /cgi-bin directory on the same server. To test this code, 
// change the URL to point to an actual script on your server.

myLoadVars.onLoad = function (success) {
  // If the data loads, display the variable in a 
  // text field named output_txt on  _root.
  if (success) {
    _root.output_txt.text = this.myVar;

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 statement to list them and their values, as shown in Recipe 18.1.

18.2.4 See Also

Recipe 18.1 and Recipe 19.11

    Part I: Local Recipes
    Part II: Remote Recipes