Recipe 18.4 Sending Data to a Server-Side Script

18.4.1 Problem

You want to send data from a Flash movie to a server-side script.

18.4.2 Solution

Use the LoadVars.send( ) method. Use LoadVars.sendAndLoad( ) if you expect a response.

18.4.3 Discussion

Use the LoadVars.send( ) method to send data to a server-side script if there is no need to process the result. For example, you might want to submit a web form's data to a server-side script without displaying any result from the server-side processing. However, the send( ) method does not return any confirmation that the data was received, so it isn't practical in most cases. Even if you just want to display a static message such as "Thank you for submitting the form," you need confirmation that the variables were successfully received on the server. Therefore, if you want confirmation of receipt, use the sendAndLoad( ) method instead (see Recipe 18.5).

The send( ) method sends all the enumerable properties of a LoadVars object to the specified URL. Enumerable properties are any properties that show up in a for . . . in statement for that object. The built-in methods and properties of the LoadVars class do not show up in a for . . . in statement, but custom properties do:

myLoadVars = new LoadVars(  );

// Nothing is displayed in this example.
for (var prop in myLoadVars) {

myLoadVars.myVar = "test";

// Now the myVar property shows up. When send(  ) is invoked 
// from myLoadVars, the myVar variable is sent to the specified URL.
for (var prop in myLoadVars) {

The send( ) method requires at least one parameter: the URL to which to send the variables. The URL can be absolute or relative:

// Send variables to a CGI script with an absolute URL.

// Send variables to a CGI script relative to the location of the Flash .swf file.

The server's response to a send( ) method is disregarded by the Flash movie but can be sent to a browser window. If you want the response to be displayed in a web browser, specify a target browser window as the second parameter. Use the target of "_blank" to display the response in a new browser window, or you can display the result in another, named window or frame that is already open if you know the correct target name. Neither of these two options have any effect on the browser window containing the Flash movie. However, specifying the target as "_self" or "_parent" replaces the page contained the Flash movie.

// Send variables to a CGI script and display the script's output in a new browser
// window.
myLoadVars.send("cgi-bin/submitVars.cgi", "_blank");

If you want the response to be returned to the Flash movie, use the sendAndLoad( ) method, as discussed in Recipe 18.5.

By default, the send( ) method uses the HTTP POST method to send data to the specified script. If the server-side script handles data sent using GET only, you can specify "GET" as the method parameter (the third parameter) when calling send( ):

// Send variables to a CGI script using the GET method. The null value is specified
// for the target parameter so that the response is disregarded.
myLoadVars.send("cgi-bin/submitVars.cgi", null, "GET");

Note that the method parameter must be the literal "GET" or "POST". Variables or expressions are not allowed for the method.

18.4.4 See Also

Recipe 18.5. See Recipe 6.13 for details on for . . . in loops. See Recipe 11.13, Recipe 11.17, and Recipe 11.19 for ways to transmit data associated with a form.

    Part I: Local Recipes
    Part II: Remote Recipes