Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Display some text from a php file in a flash movie

just need a really simple starting point



3:05 pm on Apr 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

I have spent (wasted?) all afternoon trawling through various tutorials about displaying data from a mysql database in a flash movie via PHP, but with no success whatsoever, just blank screens and flash compiler errors.

If I can just get to the stage of writing "Hello World!" with PHP and getting it to show up in a flash movie, this would make me very happy. Can anyone give me some pointers?

I don't want to do anything really complicated yet, I'm happy to work the rest out myself, but at the moment I'm just not convinced that I can even get flash and php to communicate at all :(

For info I'm using CS3.


3:56 pm on Apr 14, 2009 (gmt 0)

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Try the following:

Setup a dynamic text field and give it a name, then covert it to a movie clip with the following code:

onClipEvent (load) {
loadVariables("http://localhost/path/to/file.php", this, "GET");

Then in file.php you want to echo the variable name you used for the dynamic text field and then the content it is supposed to contain. For example:

echo 'var_name=Hello';

See if you can't mess around with that to get things working.


1:38 pm on Apr 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

Thanks eelixduppy. I tried this but unfortunately I'm still looking at a blank flash movie :(

I had to tell flash to use Actionscript 1 & 2 rather than 3 in order to apply actions to the movie clip itself, which is presumably what I needed to do?

Any ideas?


2:24 pm on Apr 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

You'll be glad to hear that I finally found some code that worked (blatantly nicked from another forum I'm afraid!)


function loadFile():void
var request:URLRequest=new URLRequest();
var loader:URLLoader=new URLLoader();
catch (error:Error)
trace('Document did not load');

function addListeners(d:IEventDispatcher):void

function onComplete(e:Event):void
var loader:URLLoader=URLLoader(e.target);
var vars:URLVariables=new URLVariables(loader.data);

This code works for CS3 and goes in an actions layer. You also need to create a dynamic text field named my_txt, and the content of myfile.txt needs to be something along the lines of


The next step is to try and get it to read from a php file rather than a text file, but it doesn't seem to work with this code just swapping the txt file for a php one. Perhaps one of you flash gurus can tell me why?


3:08 pm on Apr 15, 2009 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Note that in both cases, you are loading a variable.

So whether it's a text file, perl or PHP, Flash is expecting a variable, not just page output. Try something like this for the php script:

echo "var_from_myfile=someinfo";

Better yet, combine your (painful!) efforts with a clean deployment of Flash using SWFObject's addVariable parameter in the PHP output. Normally this is done in a static page, but you can output it dynamically.

Be sure to download and have the SWFObject libary in the specified location:

$swf = '/js/swfobject.js';
$myflash = '/flash/myflash.swf';
$variable_content = 'Hello CRUEL WORLD!';
var $msg ="
<div id=\"flash-placeholder\">
This will be REPLACED by your Flash onload.
Use an image $flashwidth x $flashheight.
<script type=\"text/javascript\" src=\"$swf\"></script>
<script type=\"text/javascript\">
var flashobject = new SWFObject('$myflash', '$flashid', '$flashwidth', '$flashheight', '6', '#ffffff');
flashobject.addParam('wmode', 'transparent');
flashobject.addVariable('var_from_myfile', '$variable_content');
window.onload = function() {
if (document.getElementById('flash-placeholder')) { flashobject.write('flash-placeholder'); }
echo $msg; # or print $msg;

You may have to fiddle with the quoting, I may have errors there as I just typed it out without testing.

I don't think you'll have to concatenate these

, '.$flashheight.',

because the actual "wrapper" is the double quote which will interpolate the PHP variables, and the single quotes are output for the Javascript.

But this will work, and probably with either example above or the simplest sample in the Flash help files using loadVariables or even getURL().


2:46 pm on Apr 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

Thanks Rocknbil. I actually managed to get my previous code working in the end, the problem was that I was compiling the flash locally, and was thrown by the error message I got when I changed the reference from the .txt to the .php file. But then I remembered that php was unlikely to work from my desktop... :"-)

However, I'm sure you're right in there is a better way to do this, and funnily enough I know I need to sort out a better way of embedding flash generally, which I asked about in another thread [webmasterworld.com] and they are suggesting the same as you - swfobject.

I have tried this using the code you posted and the one suggested over there (similar to yours but without the variable stuff) but unfortunately I'm still just getting the placeholder text coming up, and not the flash movie itself. I have double checked my paths to files and all seems OK (hopefully it's not just Friday afternoon st00pid syndrome) - any ideas what could be wrong?

[edit]fixed formatting[/edit]

[edited by: HelenDev at 2:47 pm (utc) on April 17, 2009]


4:00 pm on Apr 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

I followed this tutorial to the letter Embedding Flash with SWFObject 2.0 [gotoandlearn.com], and it looked really good and simple, and just what I want to do. This still isn't working for me of course!

Bizarrely, the HTML I generated works fine for embedding a flash movie, but just not the one I created from the tutorial! It works great with my other flash movies. I have viewed the flash movie created from the tutorial directly on the web and it works fine on it's own (although the flashvars don't get passed of course).

What on earth could be going on here?! <bangs head against wall>


9:36 am on Apr 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

I fear it was indeed Friday afternoon st00pid syndrome. This morning, the files I created after following the tutorial are working :"-)

Thanks for all your help guys :)


Featured Threads

Hot Threads This Week

Hot Threads This Month