Recipe 19.7. Sending Variables and Handling a Returned Result


Problem

You want to send variables to a server-side script and handle the results of the server-side processing.

Solution

Use the URLLoader.load( ) method coupled with a URLRequest instance that has its data property set.

Discussion

You should use the URLLoader.load( ) method when you want to send variables to a server-side script and have the results returned to Flash. An example of such a scenario is a Flash storefront for a product catalog whose data is stored in a database. Typically, items are categorized. When a user selects a category, the Flash movie might send the selected category ID to a server-side script and expect the script to return all the items in the category.

The URLLoader.load( ) method sends variables to a server-side script in the same way that the sendToURL( ) and navigateToURL( ) methods do. The data set in the URLRequest instance passed to the load( ) method is sent to the script at the specified URL. Handling results is exactly the same as discussed in Recipe 19.2. When the complete event is handled, the data property of the URLLoader that dispatched the event contains the result of the script processing.

Here is a complete example that sends data to a script that returns URL-encoded values and places the result in a text field on the screen:

package {   import flash.display.*;   import flash.text.*;   import flash.events.*   import flash.net.*;      public class SendAndLoadExample extends Sprite {     private var _output:TextField;        public function SendAndLoadExample(  ) {       initializeOutput(  );       sendData(  );     }          private function initializeOutput(  ):void {       _output = new TextField();       _output.width = stage.stageWidth;       _output.height = stage.stageHeight;              addChild( output );         }          private function sendData(  ):Void {       // Create a URLRequest to contain the data to send        // to process.cfm       var request:URLRequest = new URLRequest( "process.cfm" );              // Create name-value pairs to send to the server       var variables:URLVariables = new URLVariables(  );       variables.method = "getProductDetail"       variables.productId = 2;       request.data = variables;              // Create a URLLoader to send the data and receive a        // response       var loader:URLLoader = new URLLoader(  );              // Expect the script to return URL-encoded variables       loader.dataFormat = DataFormat.VARIABLES;              // Listen for the complete event to read the server response       loader.addEventListener( Event.COMPLETE, handleComplete );              // Send the data in the URLRequest off to the script       loader.load( request );     }          private function handleComplete( event:Event ):void {       var loader:URLLoader = URLLoader( event.target );              // Expect the script to return name and description variables.       // Display these values in a text field on the screen.       _output.text = "Name: " + loader.data.name + "\n"                      + "Description: " + loader.data.description;     }   } }

See Also

Recipes 19.1, 19.2, 19.3, 19.4, and 19.6




ActionScript 3. 0 Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2007
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net