Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Four Ways to Transfer Data Between Flash and PHP : Page 2

Discover the options for implementing bidirectional communications between browser-based Flash forms and server-side PHP code.


advertisement
Flash to PHP Using getURL()
Although Flash offers additional techniques for sharing data with server languages such as PHP, Java, ColdFusion, ASP, and so on, this article covers only the most commonly-used techniques: the getURL method and the LoadVars class. To get data to PHP using ActionScript, you can call the getURL method, which loads a page into the browser from a specified URL. The syntax of getURL is:

getURL(url [, window [, "variables"]])

The parameters to getURL are:

  • url—the URL to load.
  • window—The window to accept any response from the server, which can be _self (default), _blank, _parent, or _top.
  • variables—A string that specifies the HTTP method to use: either GET or POST.
For example, to send the data from the sample form you can call getURL like this:

on (release) { var request="form.php" + "?username="+_parent.name.text+ "&useremail="+_parent.email.text+ "&usersex="+_parent.radioGroup.selection.label+ "&usercountry="+_parent.country.value; var selectedCheckboxes=checkboxArray.join(","); var listSelection = new Array(); var listLabels = ""; listSelection = _parent.format.selectedItems; for(j = 0; j< listSelection.length; j++) { listLabels = listLabels + listSelection[j].label + ","; } request=request+"&checkboxes="+selectedCheckboxes; request=request+"&list="+listLabels; request=request+"&observation="+_parent.observation.text; getURL(request,"_blank","GET"); }

Author's Note: The preceding code goes in the Actions panel of the Submit button.

The other common technique for sending data from Flash to PHP uses the LoadVars class, an ActionScript class that exposes three methods for loading and sending data to a server language: load, send, and sendAndLoad.

The send method syntax is:



lv_object.send(url [,target, method])

In the preceding code, the lv object is an instance of the LoadVars class. The parameters to the send method are:

  • url—the URL that receives the data
  • target—the window target to accept any answer from the server, which can be _self (default), _blank, _parent, or _top.
  • method— A string that specifies the HTTP method to use: either GET or POST.
For this example, you want to call the send method when a user clicks the Submit button, by placing the following code in the Submit button's Actions panel:

on (release) { var selectedCheckboxes=checkboxArray.join(","); var listSelection = new Array(); var listLabels = ""; listSelection = _parent.format.selectedItems; for(j = 0; j< listSelection.length; j++) { listLabels = listLabels + listSelection[j].label + ","; } sendData = new LoadVars(); sendData.username = _parent.name.text; sendData.useremail= _parent.email.text; sendData.usersex= _parent.radioGroup.selection.label; sendData.usercountry= _parent.country.value; sendData.checkboxes= selectedCheckboxes; sendData.list= listLabels; sendData.observation= _parent.observation.text; sendData.send("form.php","_blank","GET"); }

Receiving Flash Data in PHP
At this point, when you click the Submit button, the form delivers the data to a PHP page on the server. Now you need to write the logic. The page form.php (available in the downloadable sample code) extracts the GET (or POST) data from Flash and implements the business logic. For the purposes of this example, it simply returns an HTML table that contains the data received, as proof that the PHP script was able to access the data sent by the Flash form:

<html> <head>form.php</head> <body> <table border="2"> <tr> <td><strong>Variable Name</strong></td> <td><strong>Variable Value</strong></td> </tr> <tr> <td>username</td> <td><?php echo $_GET['username']; ?></td> </tr> <tr> <td>useremail</td> <td><?php echo $_GET['useremail']; ?></td> </tr> <tr> <td>usersex</td> <td><?php echo $_GET['usersex']; ?></td> </tr> <tr> <td>usercountry</td> <td><?php echo $_GET['usercountry']; ?></td> </tr> <tr> <td>checkboxes</td> <td><?php echo $_GET['checkboxes']; ?></td> </tr> <tr> <td>list</td> <td><?php echo $_GET['list']; ?></td> </tr> <tr> <td>observation</td> <td><?php echo $_GET['observation']; ?></td> </tr> </table> </body> </html>

You've seen two techniques for moving data from Flash to PHP. Next, you'll see how to send data from PHP to Flash.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap