DevX HomePage

Four Ways to Transfer Data Between Flash and PHP

Discover the options for implementing bidirectional communications between browser-based Flash forms and server-side PHP code.
hen you need to develop a web form with a special design and great effects, you will probably elect to use Flash. But building and programming Flash forms is considerably different from building standard HTML-based forms. You program Flash using its proprietary (but JavaScript-like) ActionScript language—which is great if you're already a Flash developer—but as a PHP developer it's is more desirable to implement the business logic in PHP and use the Flash form simply to gather the data. To do that though, you need to know how to access data in the Flash form and (sometimes) how to update the Flash form from PHP as well. That's exactly what you will see how to accomplish in this article.

Creating the Flash Form
As an example, take a look at the Flash form in Figure 1. You'll see how to create, and communicate with, the form. To get started:

 
Figure 1. Sample Flash Form: The Flash form used in this article provides a test bed for bidirectional communications between Flash and PHP.
  1. Open Macromedia Flash, and choose the "New" option from the File menu.
  2. From the New Document window, General tab, choose the "Flash Document" option, and then click OK.
  3. From the Insert menu, choose the New Symbol option to create a new movie clip.
  4. Rename the movie clip flashForm, and click OK.
  5. Use the Rectangle Tool from the Tools menu to draw a rectangle of 561x493 pixels on the flashForm surface, and set the background color to #CCCCCC.
  6. Next, use the items from the Components panel (UI Components node) to reproduce the form design from Figure 1.
  7. Use the Properties panel to reproduce the component properties you can see in Figure 1 (the red arrows);
  8. Save your project as flashForm.fla.
With the Flash form created, you can move to the next task—communicating with the form.

Preparing Flash Data
Although your primary business logic will be in PHP, you can't avoid ActionScript altogether; you need to write a little ActionScript code to retrieve and prepare the data to be sent to PHP. That means you need to know how to extract the data for each type of Flash component (just as in JavaScript, the code you use to get data from a control depends on the control type). For the various controls, here's the ActionScript you'll need:

DevX is a division of Internet.com.
© Copyright 2010 Internet.com. All Rights Reserved. Legal Notices