hide side navigation
    5 most recent
    Web Services
    Library's
    Component's
    Applications
    Articles
  Flash button as Flex icon  Flex form by email  Hello Remoting with AS3  AS3 Saving data from Flash  AS3 Loading data into Flash  Fire Effect  Contact form  Dragable buttons  Hello World with openamf  Loading helper classes  Upload with Flash 8  Transitions effects  Snapshot with Flash 8  Hello World Remoting AS2  Flash AS2 Remoting Connector  Saving data from Flash  Loading data into Flash  FlashCom & Remoting login  Cell Renderer API  Editing a table using remoting components  Flash MX2004 web service classes  Browsing a catalog  amfphp Documentation  Hello World Remoting  Online Store with AMFPHP  Flash clients for Web Services  Web Service Walk Though with NuSoap  Popup windows in flash with javascript  Installing Apache/PHP  MoreOver News Feeds  Load Edit Save Text Files via CGI  Save Movie Clip Postion via PHP and MySQL
Current Page (5) << Previous Page | Next Page >>  View Article Example >> 1 | 2 | 3 | 4 | 5 | 6 | 7
Untitled Document

5. Saving data to databases using Remoting (amfphp-CFM) examples

Remoting is a Macromedia system that give Flash Movies direct access to server functionalities, like database access, email sending, file read&write and so on. Trough Remoting, instead of use name/value pairs, Flash use AMF (ActionScript Message Format) to transfer data packets from/to back-end. It means that you can send and receive any kind of ActionScript data whit out the need of using name/value pairs. Even you can receive arrays of arrays and other kinds of data, like XML in example. But how do you output AMF packets from your back-end language? Well, using some server extensions or a ColdFusion server, who has native support for Remoting. If you don't have a Coldfusion server, there are extensions for .Net, and Java (JRun) environments. Also there are some free back-end libraries to build AMF packets, like amfphp, remoting for Java and Perl. In order to know about amfphp Remoting basis (installation, setup, examples), you can read "Hello World Remoting". About ColdFusion, Macromedia offers several tutorials about Flash/ColdFusion integration trough Remoting, check Macromedia Devnet

----------------------------------------------------------------------------------------------------
TIP
There are others serialize/unserialize solutions for data transfer between Flash and the back-end script, like WDDX or PHP Object, but all of them needs to add some client parse process in order to unpack data
----------------------------------------------------------------------------------------------------

Here we can follow the same principles as the database data loading: while the back-end script changes for different systems, the Flash side remains almost the same. So we will look first in the Flash side, and then give a couple of Remoting scripts (Coldfusion and amfphp)

For this example we will use again the same database. We can pass data as done in previous examples, the layout remains the same:

Flash Remoting side

In order to use Remoting, you need Remoting components. You can download it from Macromedia site. Whit out this components you can't talk to your back-end. We will use the same service as the previous tutorial, just adding an insert method. The layour is the same as previous examples. So the first two lines load necessary Remoting library's:


        
#include "NetServices.as"
        #include "NetDebug.as"

The first one include the necessary library, while the second is only for debugging purposes. As far as your application is working fine, you can delete this line. Ok, having all the necessary tools, now we need to setup the gateway and look for the service. This means the location of a file with the same name as the service, so in example, if I want to setup a service named "News", I need to create necessary methods in a file named news.cfc (Coldfusion) or news.php (amfphp). The difference between both methods are minimal in the Flash side. Here's the Remoting connection:

CFC script


        
//those using ColdFusion on port 8500, should use http://localhost:8500/flashservices/gateway
        
NetServices.setDefaultGatewayUrl("http://localhost/flashservices/gateway")
        var 
gatewayConnection NetServices.createGatewayConnection()
        
service gatewayConnection.getService("myNews.News"this)

AMFphp script


        NetServices
.setDefaultGatewayUrl("http://localhost/myFolder/gateway.php")
        var 
gatewayConnection NetServices.createGatewayConnection()
        
service gatewayConnection.getService("News"this)

  

There are only few differences between both scripts. In the first case, we omit the extension of the file, while in the second we use the extension php in the first line. This line tells the movie where to find the necessary services. Coldfusion have build-in support and you don't need to do nothing special in the server side about this, but using amfphp you need to write a special gateway.php file. This gateway file could be anywhere as far as the movie can locate it, so I have change the path to myFolder to remark the difference (but change to match yours when testing !)

The second line instantiate the connection and the third look for the service we're going to use, as previous exposed, a file in the server. The CFC method use a connection string myNews.News, that looks for a file named news.cfc inside myNews folder. The amfphp version set the name of the folder inside the gateway.php file, so it's not necessary to use any additional information in the string.

Now we need to open the NetConnection Debugger (Menu Window - View - NetConnection Debugger) This tool allow developers to see the talk between Flash and Remoting, tracing all actions to the output. This way, you can see what's happens. Whit out this tool is really difficult to know what happens if something fails. So keep it opened in your workspace.

Now we will send data to our Remoting service, so it will populate the database. This send of data will be performed by the submit button:


        send
.onPress = function(){
           
service.insertTitles(Title.textComments.textImage.text})
        }

  

And the service will return a result that we should catch. There are different ways of doing this. In example, we can pass an object to handle results, but here we simply sets current timeline as the default place to receive data. Each time a method returns data, a function with the same name of the function plus Result is called, so writing a function in the correct scope handle it. If the service name would be "getComments", the result handler name would be getComments_Result and so on. Each time you call a method, you need to add a result function to handle results.

So inside the Result function we simply put this:


    insertTitles_Result 
= function(msg){
      if(
msg=="Ok")  {
          
gotoAndStop(2)
        
status.text "Submited data was saved"
       
} else status.text "Error in saving submitted data"
    
}

  

That's it. Now let's see the back-end side

Current Page (5) << Previous Page | Next Page >> 1 | 2 | 3 | 4 | 5 | 6 | 7