17.2. Creating a Flash Fill-in FormOur example includes all of the necessary components of a Flash form, cited earlier, but stripped down to the simplest level. This tutorial demonstrates how to send a single text field variable from Flash to a Perl script, named echo.pl, and how to receive a response in Flash back from echo.pl. Functioning versions of the example files are available from the online Code Depot. Let's get to it, shall we? 17.2.1. Building the FrontendUnlike HTML, Flash does not have an integrated mechanism for the creation of forms. In HTML, creating pull-down menus and radio buttons is simply a matter of using the <SELECT>, <OPTION>, and <INPUT TYPE="RADIO"> tags. In Flash, those devices must be built by hand. Flash's only built-in form device is the user-input text field (the equivalent of HTML's <INPUT TYPE="TEXT"> or <INPUT TYPE="TEXTAREA">).
In our form, we'll have a user-input text field and a Submit button. We'll place these two elements into a movie clip so that we can easily identify the variables to send to the server. First, we'll create a new document and the formClip movie clip, as follows:
Next, we'll add a user-input text field to formClip by following these steps:
Now we'll assign our input text field a default value, ensuring that it will be sent to the server even if the user doesn't enter any data:
Finally, we add a Submit button to formClip by following these steps:
17.2.2. Sending Data to the ServerThere are many ways to send data from Flash to a server application, including the loadVariables( ), getURL( ), loadMovie( ), XML.load( ), XML.sendAndLoad( ), and XMLSocket.send( ) methods. In our example, we'll use the loadVariables( ) method. For information on the others, see Part III, "Language Reference". In the previous section, we placed a user-input text field and a Submit button in the movie clip formClip. To make that Submit button send formClip's variables to echo.pl when clicked, follow these steps:
The code on our Submit button uses loadVariables( ) to send the variables of formClip to echo.pl, and it causes our response clip to display a loading message. We'll build the response clip a little later. For now, let's examine how the loadVariables( ) invocation works. The first parameter of the loadVariables( ) invocation should specify the location of echo.pl on your server (server-side scripts are typically stored in a folder named cgi-bin). Be sure to set the location correctly according to your server's domain name and directory structure. When loadVariables( ) executes, all the variables in formClip are sent to that location. The second parameter of the loadVariables( ) invocation indicates the path to the clip in which we will store the return value sent by echo.pl, namely "_root.response". The third parameter of the loadVariables( ) invocation specifies the HTTP method we're using to submit formClip's variables to the server -- the GET method in this case. ActionScript supports both GET and POST operations, as described under loadVariables( ) in Part III, "Language Reference". 17.2.3. The Perl Script, echo.plWhen the user clicks the Submit button in our movie's formClip, Flash initiates an HTTP GET request. This request executes the Perl script echo.pl. In order for our form to work, echo.pl must be placed in a CGI-enabled directory of a web server and configured by the server's administrator as follows:
Example 17-1 shows the source code for echo.pl. Note that the # character indicates a comment in Perl. Example 17-1. The Source Code of echo.pl#! /usr/local/bin/perl #------------------------------------------------------------------------------- # Name: Simple Flash Echo # Version: 1.2.0 # Author: Derek Clayton derek_clayton@iceinc.com # Description: Echoes back name/value pairs received from a Flash GET or POST. #------------------------------------------------------------------------------- # MAIN #------------------------------------------------------------------------------- use CGI; # Use the CGI.pm for easy parsing $query = new CGI; # Query object $echoString = "output="; # Initialize our output string &getInput; # Get the input received from Flash &writeResponse; # Write the response back to Flash exit; # Exit the script #------------------------------------------------------------------------------- sub getInput { # For each key get the associated value and add to the echo string foreach $key ($query->param) { $value = $query->param($key); $echoString .= "$key:$value\n"; } # Remove the trailing newline (\n) before writing response chomp($echoString); } sub writeResponse { # Set content type for Flash print "Content-type: application/x-www-urlform-encoded\n\n"; # Write the output print $echoString; } The echo.pl script performs three general tasks:
Upon receiving the string returned by echo.pl, Flash automatically interprets it as a series of URL-encoded variables (as described in the entry for loadVariables( ) in Part III, "Language Reference"). Hence, output becomes a variable on the response clip's timeline. By examining the value of output in our Flash movie, we can see which variable names and values were originally sent to echo.pl. Obviously echo.pl is not the most interesting web application in the world. It is only a proof of concept. When applied, however, the concept can have interesting and powerful results. For an example of a more fully developed Perl system, see the flat file database sample available at the online Code Depot under "Server Communication." 17.2.4. Receiving Results from the ServerRecall that when we sent the variables of formClip to echo.pl, we requested that echo.pl 's return value be stored in the movie clip response: loadVariables ("http://www.yourserver.com/cgi-bin/echo.pl", "_root.response", "GET"); We'll now build that response clip; it has three states: idle, loading, and done loading. In the idle state, response is invisible to the user, waiting for data to begin loading. In the loading state, response indicates to the user that data has been submitted to the server and that Flash is waiting for a reply. In the done loading state, response has received the server's reply and displays that reply to the user via a text field. The three states of response govern its timeline structure. Each state is represented by a labeled keyframe: idle, loading, and doneLoading. The frame displayed is dictated as follows:
To build response, we follow these steps:
Now let's add the data event handler that will be triggered when the server has finished sending its data to Flash. Follow these steps:
When Flash receives the content sent by echo.pl, response's data event handler executes automatically. In the data handler, we move response's playhead to the doneLoading frame, and then we display the value of output (supplied by echo.pl ) in outputField. Our use of the data event handler ensures that the output variable will always be loaded before we attempt to display its value in the outputField text field. All that's left to do is try your form out! Export your movie, enter some text in the input text field, and click the Submit button. If the form doesn't work the first time, make sure your server script is configured properly. And remember, you can study the functional version posted at the online Code Depot. Reader Exercise: Try adding a Reset button to your form that clears the value of its input field. Create a new button in the formClip and attach the following code to it: on (release) { input = ""; } Also, try adding more than one input field to your form; the Perl script will faithfully return as many variables as you send it. Can you split them up and display each one's value separately? Copyright © 2002 O'Reilly & Associates. All rights reserved. |
|