发新话题
打印

PHP Vatiable into Flash

PHP Vatiable into Flash
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:

on(release){
addressVars = new LoadVars();
addressVars.load("http://localhost/testing/addressbook.php");
addressVars.onLoad = function() {
_root.theName = addressVars.theName;
_root.theEmail = addressVars.theEmail;
}
}
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:

<?php
$theName = "Kev" ;
$theEmail = "kevleitch@gmail.com" ;

$passIn = "" ;
$passIn .= "theName=" . $theName . "&" ;
$passIn .= "theEmail=" . $theEmail ;

echo $passIn ;
?>
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.

TOP

发新话题