• Home
  • About
  • Contact Us
  • Gallery
  • Video
  • Archives
  • Site Map
Grab the RSS feed

FlashBannerOnline

FlashBannerOnline
  • Home
  • FlashBannerOnline
  • Free Downloads
  • General
  • Tutorials
    • Adobe AIR
    • CSS
    • Fireworks
    • Flash
      • ActionScript
    • FLEX
    • Javascript
      • JQuery
      • MooTools
    • PHP

Using ActionScript 3.0 with PHP Loading External Variables

  • ActionScript, Flash, Tutorials

    Posted on October 13th, 2009

    Written by Behrouz Pooladrag

    Related Posts

    • Using URLLoader to send and load Data to Server Used FLEX
    • Advanced Flash AS3 Honeycomb Preloader
    • Loading XML data using ActionScript 3.0

    Tags

    "load variables" in as3, "load variables" in php in as3, access php file from one server to another server in as3, access php function actionscript, action script 3 expose varibale for php, action script load data from php, actionscript, actionscript 3 con php, actionscript 3 get data php, actionscript 3 http_build_query(, actionscript 3 php menu load variable, actionscript 3 sharing importing php variables, actionscript 3 urlloaderdataformat.variables escape, actionscript 3 with php, actionscript 3 working with php, actionscript 3.0 load variables php, actionscript 3.0 loadvariables php, actionscript ad banner loader swf, actionscript and php web services, actionscript loading flash var, actionscript loadvariables result is html code?, actionscript not loading php, actionscript passing variables from php, actionscript receiving outisde variables, actionscript send get function, as 3.0 with php, AS3, as3 get variables "from php", as3 load php variable, as3 load php vars class, as3 load variables from php using parameters, as3 load variables from script, as3 mysql data, as3 php loading external variables, as3 php vars output as xml, as3 receive variables from php, as3 send variables php mysql, as3 sendandget, as3 urlloader get variables from php, as3 urlrequest limit, as3 urlrequestmethod.get, as3 working with external php, as3.0 external php variables, banco de dados em as3, code flash send variable to server php, combine php and actionscript 4, database, dynamic php variable as3.0, external php variables as3, external text to array in flash as3, external variables in actionscript, flas as3 php loading external variables post, Flash, flash as 3.0 rss reader, flash as3 loadvariables from php, flash as3 xml through php, flash banner variables, flash external variable load, flash load php script with arguments, flash loader php get variable, flash loading external variables into array, flash php mysql action script load variable, flash tutorials as 3.0 loading variables from flash to php, flash variables in array php, flash with php as 3.0 load vars, flex 3 php http_build_query, flex navigatetourl variables size object, flex receive post variable, get data using actionscript 3.0, get variable data from web pages xml output, getting variable from php to as3, getting variables from php as3, how can we send and recieve data using php in flex, how communicate as3.0 with php, how send variable to a page when loading, how to add data in mysql using php in actionscript3.0, how to communicate with server using actionscript, how to create advance flash websites in action script 3.0, how to load .obj into flex as3.0 with example, http_build_query action script, import external variable php, import php in actionscript 3, import variable as3 php, import variables php to actionscript 3, load array php to flash as3, load data in variable php, load external php variables as3, load external php variables from as3, load external web site into php, load object into vars php, load php into flash actionscript 3, load php to as3 variables, load php variable flash actionscript 3 array, load variable flash with php, load variable from server actionscript 3, load variables from actionscript 3 to php, load variables php actionscript 3.0, load(request) php as3, loading external php variables as3, loading external variable in flash into array, loading external variables in flash using php, loading text from php using as3, loading variables php flash as3 action script 3, loading vars in actionscript 3 using php and mysql, loadobject() php, loadvariables, loadvariables actionscript example, parsing external data string in array in as3, pass a php variable to actionscript 3, passing data to as3 from php, passing variable php actionscript 3, passing variables from flash as3 to php database, PHP, php actionscript 3.0 retrieve variables from external script, php arabic contact by use as2, php as3 urlencode, php external variables in function, php inside as3, php variable in flash as3.0, php variable into as 3.0, php variables email as3, post variables send actionscript 3.0 php, receive data from php to flash in as3, send and get value to php using as3, send variable arrays to and from flash php, send variable external interface as3, send variables to flex from php, sending data in xml format to server in actionscript 3.0, sending values from flash to php as 3.0, sending variables from php to flash as3, transfer value from actionscript to php, upload data into flash using xml in as3.0, url loader as3 urlencode, urlencoded php code as3, urlrequestmethod.get php mysql, use php code inside actionscrip code, using actionscript 3.0 with php, using actionscript 3.0 with php part 1: loading external variables, vars from php to actionscript 3, web, what is limit for urlrequest using post method in as3
    Using ActionScript 3.0 with PHP Loading External Variables

    Creating dynamic websites which combine the power of Flash or Flex with PHP is easier than ever with ActionScript 3.0.. Utilizing the networking power of the Flash player we can create next-generation user interfaces which go well beyond static web pages or simple animations and instead act as the front-end for powerful applications. These applications can interface with databases and web services, giving us the ability to add new features, create ‘mash-ups’, or previously unheard-of platforms for user interaction. The sky is the limit! So let’s get started.

    Server Requirements

    If you are creating web applications which run Flash and PHP obviously your server needs to have PHP installed. If you plan on creating many dynamic sites or applications going forward, you will probably want server space which allows for unlimited databases, has PHP5 and MySQL, and gives you the ability to install programs on the web server. If you are using shared hosting, that is fine, but keep in mind that you will probably want to go with a Virtual Dedicated solution at some point, simply because of the flexibility and responsiveness you receive from having your “own” server. For a virtual dedicated server I personally use GoDaddy, as I now have unlimited domains, unlimited databases, and root access to my server, so I can do a bit of shell scripting now and again. On top of that it is super-responsive which is really important for creating Flash applications which utilize networking. Otherwise if you want to pay less but still want unlimited domains and a fairly responsive system, I would go with Blue Host in North America or 1 and 1 for Europe (or other regions), and a shared hosting plan. I have experience with all three of these companies and the support is certainly top-notch.

    Communicating with a Web Server

    Let’s discuss three ways in which we might send data back and forth between a web server and Flash. These are by no means the only ways to do so, but they are fairly common, and you will find them to be popular ways to accomplish networking tasks.

    Using GET or POST and Variables

    If you are used to working with HTML front-ends and PHP (or another scripting language), this will be familiar to you. Using the GET or POST method, you can send and receive data from server-side scripts. In this scenario, we often output our data in some sort of url-encoded string, which contains variable “name and value” pairs. This is the method we will cover in this article.

    Using XML

    XML is one of the most important languages for communication between application, servers, and services on the Internet. One good reason to work with XML for loading data from a web server is that you will generally not have to know how the data is being processed, you will just have to know something about the particular XML formatting being used, so that you can process the data.

    Using Flash Remoting

    For more sophisticated integration with a server, you may want to utilize Flash remoting. If you are using Flex, you may work with Flex Data Services. If you would like an open-source solution, you might want to look into AMFPHP. We will not cover Flash remoting in this series.

    Loading Variables

    The first thing we will do is use GET to load variables from a PHP script. We will create a simple PHP document, called “dataLayer.php”. This page will act as our main communicator between our server and Flash.

    File: dataLayer.php

    
    <?php
    
    $returnVars = array();
    $returnVars['username'] = "testuser";
    $returnVars['email'] = "test@hotmail.com";
    $returnString = http_build_query($returnVars);
    
    //send variables back to Flash
    echo $returnString;
    
    ?>
    

    So here we have our data layer. It consists of two variables: username and email. These variables are encoded using the http_build_query() function to send a properly formatted, compliant variable set from the server. Notice that this function takes an array as a parameter. It then just builds out of string in the format of name / value pairs, like so “varname=value&varname2=value”. It’s very handy.

    Next, we will instruct the Flash player to load this data so that we can work with it.

    Loading the Variables into Flash

    Now let’s write some ActionScript to load our variables into Flash. We will do this in a document class which will be the main point of entry for our Flash movie.  Our document class will be called “Main.as” and we will utilize it with an .fla file called “main.fla”.

    Complete Main.as code below. You can also download it here: data-loader.zip

    
    package {
    
    import flash.display.MovieClip;
    import flash.events.*;
    import flash.net.*;
    public class Main extends MovieClip {
    
    public function Main()  {
    
    // Prepare request
    
    var request:URLRequest = new URLRequest("http://www.yourserver.com/dataLayer.php");
    request.method = URLRequestMethod.GET;
    
    var loader:URLLoader = new URLLoader();
    loader.dataFormat = URLLoaderDataFormat.VARIABLES;
    loader.addEventListener(Event.COMPLETE, completeHandler);
    loader.load(request);
    
    function completeHandler(evt:Event) {
    
     var username = evt.target.data.username;
     var email = evt.target.data.email;
    
     trace ('username is ' + username);
    
     trace ('email is ' + email);
    
    }
    
    }
    
    }
    
    }
    

    You will want to replace the path (”http://www.yourserver.com/”) with your actual server and directory.

    This code is very simple. You will see that we have imported appropriate classes for working with networking, and that our class (being the document class) extends MovieClip.

    The first part of our constructor class prepares a request, and it uses the GET method to do so. Here we make an instance of the URLRequest object, passing it the actual web path to our php file.

    
    var request:URLRequest = new URLRequest("http://www.yourserver.com/dataLayer.php");
    request.method = URLRequestMethod.GET;
    

    We then make a URLLoader object, specifying its dataFormat property to be “VARIABLES”. This will ensure that we can bring the variables output by PHP into Flash using the same names, and those variables can easily integrate into the scope of our Flash movie.

    
    var loader:URLLoader = new URLLoader();
    loader.dataFormat = URLLoaderDataFormat.VARIABLES;
    

    Next we define an event handler, which is key for handling the loading of the variables. We are listening for the “COMPLETE” event, which is an event thrown by the URLLoader object that we just created. Notice that we define a handler, called completeHandler, which will do the work of processing our variables for us. This event handler will take the form of a “nested” function – a new feature in ActionScript 3 which allows us to place functions inside of functions. (I love this). After we define a handler, we then call the load() method of the URLLoader object, passing it the request objet.

    
    loader.addEventListener(Event.COMPLETE, completeHandler);
    loader.load(request);
    

    Our completeHandler() function does the work of getting our variables from the COMPLETE event. We created a parameter called “evt”, which is populated by the event object itself when this function is called. We get the variables from PHP using the “evt” object and the following syntax: “evt.target.data.variableName“. This allows us to specify the exact variable we want to access. Lastly, we trace this data to the output window.

    
    function completeHandler(evt:Event) {
    
     var username = evt.target.data.username;
     var email = evt.target.data.email;
     trace ('username is ' + username);
     trace ('email is ' + email);
    
    }
    

    And that’s it! This is a very simple example, but it should get you going with getting data from your PHP scripts into Flash. Obviously there is a lot more to learn about communicating with a server from Flash, but in any case, AS3 makes it simple! In the next tutorial we will talk about sending and receiving data using POST. Good luck!

    This entry was posted on Tuesday, October 13th, 2009 at 10:09 am and is filed under ActionScript, Flash, Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

    You might also like

    Sample Loading Text and XML with URLLoader In previous versions of ActionScript, there were a couple of classes who had the capability of loading external...
    Loading Text and XML with URLLoader in AS3 In previous versions of ActionScript, there were a couple of classes who had the capability of loading external...
    Using URLLoader to send and load Data to Server Used FLEX Here is a very simple example of two way communication with database using Flex and PHP. In this example, we are...
    Creating a Flash Preloader Using AS3 This tutorial will teach you how to create a very simple preloader for your Flash movies using ActionScript 3.0....
  • 12 Comments

    Take a look at some of the responses we've had to this article.

    1. Sandy
      Posted on August 18th

      I wnat evt.target.data.username with loop. Thank you very much

    2. Behrouz Pooladrag
      Posted on August 18th

      you need use username_1 , username_2 ,… and get it by a loop …
      by php create your data by a number for get it in actionscript like As above type… :D

    3. sander
      Posted on September 28th

      Tnx for the tutorial! I was wondering is there a way you can get the id of the logged in user of a site to give that to flash?

    4. Behrouz Pooladrag
      Posted on September 28th

      yes :D
      you can use any php code and output result to flash
      be successful ;)

    5. Arjun
      Posted on October 9th

      How to get data from database(mysql) to as 3.0

    6. Behrouz Pooladrag
      Posted on October 9th

      inside php file you want user MYSQL query and get Data from data base and echo your data and use in flash :D
      enjoy ;)

    7. Adrian
      Posted on October 20th

      email is undefined ….
      I can not get the second variable.

    8. hani
      Posted on October 30th

      how can i pose the username and email in the .fla file? please help. i can only trace it on the output but on the exact .fla file it displays nothing. thanks.

    9. Behrouz Pooladrag
      Posted on December 22nd

      when you got data in trace , you can use there in text field or use for check in (if) or otherwise you need.
      be successful.

    10. Behrouz Pooladrag
      Posted on December 22nd

      use urlencoded php code for export data to get correct all variables
      be successful.

    11. LUN
      Posted on April 4th

      i got the same problem, undefinded username and email…can you give me a sample about urlencoded?

      Thanks

    12. Behrouz Pooladrag
      Posted on April 6th

      this is sample of URL Encode in as3

      var filePath : String = “http://test.com/upload/l_0,0157 e9761d#.jpg”;
      var encodedPath : String = escape(filePath);
      trace(encodedPath); //This equals “http://test.com/upload/l_0%2C0157%20%20e9761d%23.jpg”
      

      and in PHP server side:

      string urlencode ( string $str )
      

      http://php.net/manual/en/function.urlencode.php

      be succesful.

  • Post a Comment

    Let us know what you thought.

  • Name:

    Email (required):

    Website:

    Message:

  • Ad Ad

  • FlashBannerOnline.Com

  • Subscribe

    Enter your email address:

    | More

  • Blogroll
    • Random Flash Banner!
  • Tag Cloud

    • actionscript addChild addEventListener Adobe AIR Air Animation AS2 AS3 as3 preloader Banner beginFill BitmapData blog.flashbanneronline.com Class CSS Date Class draw Flash flash.net.URLLoader flash banner free flash banner generator flash banner happy new year flashbanneronline FlashBannerOnline.com flash banner online free flash player function game getChildIndex Javascript lightbox Loading Mysql new year flash banner Object Optimization package PHP Sprite tot Tweener URLLoader URLRequest web XML
  • Recent Comment
    • Behrouz Pooladrag on Flash Optimization – Freezing And Unfreezing Objects
    • Valerie on Flash Optimization – Freezing And Unfreezing Objects
    • FlashLord on RSS Reader for FlashBannerOnline Blog!
    • Behrouz Pooladrag on Using ActionScript 3.0 with PHP Loading External Variables
    • LUN on Using ActionScript 3.0 with PHP Loading External Variables
  • RSS FlashBannerOnline
    • Happy New Year 2012 & Gift!
    • New Templates Oval Light Added to Flash Banner Online
    • FlashBannerOnline started in Arabic language
    • Happy New Year 2011
    • New Templates Christmas 2011 Added at Christmas!
    • New Templates angel animate Added to Flash Banner Online
    • Added Tow New Preloader (Pro Preloader , Rotate Preloader)
    • Happy New Year , by new Theme (Christmas 2010)
  • Add to Technorati Favorites

    Subscribe
  • Google
    Custom Search

Copyright © 2009 FlashBannerOnline - Powered by Wordpress.

FBO by