Recipe 19.6. Sending
Data to a Server-Side Script
Problem
You want to send data from a Flash movie to a
server-side script.
Solution
Create a URLRequest instance that
contains the data and pass it to the script with the
flash.net.sendToURL( ) method. If you want to open the
results of the script in a specific browser window, use the
flash.net.navigateToURL( ) method. If you expect a response,
use the URLLoader.load( ) method instead, as described in
Recipes 3.12
and 19.7.
Discussion
Use the sendToUrl( ) method from the
flash.net package to send data to a server-side script if
you do not 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
sendToURL( ) 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 URLLoader.load( ) method,
as described in Recipes 3.12
and 19.7.
The URLRequest instance passed to
sendToUrl determines what (and how) data is sent to a
server-side script. Typically, the data property of the
request is set to a URLVariables instance to send name-value
pairs to the server. It can also be set to a
flash.util.ByteArray to HTTP POST binary data to a
server, or data can be set to a String to send
text, perhaps an XML-RPC request (see http://www.xmlrpc.com),
to a server. The following code snippet shows how to create a
URLRequest by using a URLVariables instance to send
name-value pairs to a server-side script:
function sendData( ):void {
// Create a request that sends data to the process.cfm page
var request:URLRequest = new URLRequest( "process.cfm" );
// Create some variables to send, someText and someNumber.
var variables:URLVariables = new URLVariables( );
variables.someText = "Some text to send";
variables.someNumber = 26.2;
// Set the data to be sent to the variables, created earlier
request.data = variables;
// Send the data to the script for processing
sendToURL( request );
}
The URL passed during the URLRequest
creation can be absolute or relative, and is governed by the Flash
Player security sandbox. For more information about security, see
Recipe
3.12.
// Set the url property to a CGI script with an absolute URL
request.url = "http://www.darronschall.com/cgi-bin/submitVars.cgi";
// Set the url property to a script relative to the location
// of the .swf file
request.url = "cgi-bin/submitVars.cgi";
The server's response to a sendToURL( )
method is disregarded by the Flash movie and data is sent
transparently behind the scenes. To send data to a script and open
the results in a specific browser window, use the navigateToURL(
) method instead. This method is almost exactly the same as
sendToURL( ) except it takes an additional parameterthe name
of the window to send the results to. Use the window name 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 for it. 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 containted in a URLRequest instance to a script
// and display the output in a new browser
navigateToURL( request, "_blank" );
If you want the response to be returned to the
Flash movie, use the URLLoader.load( ) method, as discussed
in Recipes 3.12
and 19.7.
By default, data is sent to the specified script
via the HTTP POST method when you use sendToURL( )
or navigateToURL( ). Set the method property of the
URLRequest instance containing the data to change this
behavior. Use URLRequestMethod.GET for HTTP GET
and use URLRequestMethod.POST for HTTP POST.
var request:URLRequest = new URLRequest( "cgi-bin/submit.cgi" );
// Create some variables to send
var variables:URLVariables = new URLVariables( );
variables.someText = "Post me!";
request.data = variables;
// Configure the variables to be sent via HTTP POST
request.method = URLRequestMethod.POST;
// Send the request and open the response in a new window
navigateToURL( request, "_blank" );
See Also
Recipes 3.12
and 19.7
|