PHP Variables Into Flash

13 May

I read a post over at BD4B on getting javascript data into Flash today. A lot of people expressed surprise that this wasn’t common knowledge. It is to some people. It occured to me that if people don’t know you can get client side variables into Flash then they probably don’t know you can get serverside variables in either.

Well you can. Very easily. All you have to do is URLEncode any name/value pairs you wish to pass in and you’re away. Lets take a very small example:

Go to the test page. You should see a page with a SWF in it and thats it. The movie is just a single button. Click the button and hey presto, up pops my name and email address. Those two values were inserted from a PHP script. Here’s how.

First, set up your Flash movie with a button and two dynamic text fields. I called my textfields theName and theEmail respectively.

Turn your attention to your button. This is where I’m placing all the code for this very simple example. In a real life production you’d never do what I’m about to and place all the function code in the button action. This is simple to illustrate whats happening.

OK, so on the button is the following code:

Quite simple really. First we declare a new loadVars() object. We then tell our new loadVars() object (which I called ‘addressVars’) which script to load via the load method. I told it to load the script ‘addressbook.php’.

Lastly, we specify an onLoad function to tell our movie what to do with the loaded data. I told it to put the passed in values from the PHP script into our waiting dynamic textboxes. This function is vital. If you try and just match up the values without the onLoad function then Flash never knows they’ve been successfuly loaded.

Now we turn our attention to our PHP script. We have to make this script prepare our name/value pairs in a format that Flash is able to deal with – we need to URLEncode them. In other words we need to make them appear like so:

theName=Kev&theEmail=kevleitch@gmail.com

Here’s the PHP to do this:

Again, this is very straightfoward as this is a simple example. First we set our values. We then essentially build a querystring to wrap these name/value pairs up and finally we echo them out. A good way of testing to make sure you have this stage right is to run the script by itself – try it now for yourself.

And thats that! Simple. Obviously you can extend the functionality to grab data from databases or pass in arrays to build navigable recordsets within Flash. I’m quite happy to go through these more advanced capabilities if anyone so desires but for now, grab the source files for yourself.

13 Responses to “PHP Variables Into Flash”

  1. John Oxton May 13, 2005 at 22:22 #

    Ooo… Hello, now you are entering territory I know well..

    I, along with a PHP dude, built the calendar app on http://vans.co.uk/

    You might also like:

    http://ghostwire.com/go/28

    or

    http://www.amfphp.org/

  2. Kev May 13, 2005 at 22:41 #

    Nice one John. I built a PHP/MySQL/Flash calendar system called PhlashCal a looong time ago (around 2002). Sadly I can’t find the source for it anymore. Lost in the mists of time…

    I did a Flash/PHP chat once as well, I really must start hanging onto stuf like that.

  3. Niels July 22, 2005 at 14:27 #

    Hi,

    Maybe you think this something,
    I took your tutorial and build on it to get results from a Mysql database.
    Texts of php hereunder also the modified actionscript.
    I have the files if you want test.sql and .fla to make it complete
    if you give me a link or e-mail address ill send them.
    Thanks for a great tutorial it was help full to me.
    I’ve crossed out username and password and made them “root and ” & empty.
    Of coarse you’ll put your own in

    ——————————————————————————-
    And the actionscript to attach to the button.
    —————————————————————————————————————–
    on(release){
    addressVars = new LoadVars();
    addressVars.load(“php/addressbook.php”);
    addressVars.onLoad = function() {
    eerste1 = addressVars.eerste1;
    tweede1 = addressVars.tweede1;
    }
    }

    And the database

    ————————————————————————————————————
    # phpMyAdmin MySQL-Dump
    # version 2.3.0-rc3
    # http://xxxxxxx.net/phpMyAdmin/
    # http://www.phpmyadmin.net/ (download page)
    #
    # Host: localhost
    # Generatie Tijd: 22 Jul 2005 om 15:20
    # Server versie: 4.00.15
    # PHP Versie: 4.3.3
    # Database : `test`
    # ——————————————————–

    #
    # Tabel structuur voor tabel `products`
    #

    CREATE TABLE products (
    ID int(11) NOT NULL auto_increment,
    eerste varchar(50) default NULL,
    tweede varchar(50) default NULL,
    derde varchar(50) default NULL,
    PRIMARY KEY (ID)
    ) TYPE=MyISAM;

    #
    # Gegevens worden uitgevoerd voor tabel `products`
    #

    INSERT INTO products VALUES (1, ‘een’, ‘twee’, ‘drie’);
    INSERT INTO products VALUES (2, ‘vier’, ‘5’, ‘6’);

    All verry straight forward.

    Again many thanks for helping me along I thought it a good idea to go the natural step further.

    Greets,

    Niels

  4. Niels July 22, 2005 at 14:31 #

    Strangely enough the php script didn’t paste into the reply
    so here it is

    cheers,

    Niels

  5. Niels July 22, 2005 at 14:34 #

    I dont get it I think it is because it is a script
    I,ve put #around the php ofcourse you have to remouve them, so I try again
    I hope this time it prints.

    Ceers,

    Niels

    ##
    cheers,

    Niels

  6. ashish September 29, 2005 at 11:40 #

    thanks i like your solution very much. i get my problem solved from your solution.

    thanks once again.

  7. NiGhTsHaD November 21, 2005 at 20:53 #

    My problem is exactaly the opposite.
    I know how to pass variables from PHP to flash, but i don’t know how to send them from flash to PHP (because i never worked with PHP, but now i need to).

    If you, or anyone who red this message, can help me, please do it – tell me how…I implore you!!!

  8. Kev November 22, 2005 at 12:43 #

    Its very simple – you’d need a typical loadVars() object in your actionscript that referenced an actionscript variable/value then specify the location of the php script and use ‘POST’ as your method.

    Then, in your PHP script just use $myCapturedVar = $_POST['nameOfFlashVar']; and you’re done.

  9. Facro December 29, 2005 at 18:30 #

    Thanks so much for the great script, I was looking everywhere for this

  10. Sam HE February 28, 2006 at 17:03 #

    Hi,

    good stuff, hope this code can get me to get the Flash / PHP guestbook working on this Taekwon-do website..

    Reads entries from a text file, any advice please email me..

    Sam

  11. earnfun March 31, 2006 at 03:22 #

    Hi,
    I have a question.
    I pass a variable named logged_in
    print(“&logged_in=”.$logged_in_text);

    when I tested it in browser, its ok because its print &logged_in=false

    but when I tested it in dynamic field in the flash
    its return “.$logged_in_text); in the dynamic flash.

    anybody have solution please?

  12. dave April 25, 2006 at 20:35 #

    earnfun –
    it sounds like you’re testing locally, and php scripts have to be run on a server. if you upload or set up a local apache server, it should work.

Comments are closed.