homepage Welcome to WebmasterWorld Guest from 54.227.20.250
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Flash and Shockwave
Forum Library, Charter, Moderator: open

Flash and Shockwave Forum

    
Display some text from a php file in a flash movie
just need a really simple starting point
HelenDev

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3892100 posted 3:05 pm on Apr 14, 2009 (gmt 0)

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.

 

eelixduppy

WebmasterWorld Senior Member eelixduppy us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 3892100 posted 3:56 pm on Apr 14, 2009 (gmt 0)

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.

HelenDev

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3892100 posted 1:38 pm on Apr 15, 2009 (gmt 0)

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?

HelenDev

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3892100 posted 2:24 pm on Apr 15, 2009 (gmt 0)

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

loadFile();

function loadFile():void
{
var request:URLRequest=new URLRequest();
request.url='myfile.txt';
var loader:URLLoader=new URLLoader();
addListeners(loader);
try
{
loader.load(request);
}
catch (error:Error)
{
trace('Document did not load');
}
}

function addListeners(d:IEventDispatcher):void
{
d.addEventListener(Event.COMPLETE,onComplete);
}

function onComplete(e:Event):void
{
var loader:URLLoader=URLLoader(e.target);
var vars:URLVariables=new URLVariables(loader.data);
my_txt.text=vars.var_from_myfile;
}

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

var_from_myfile=someinfo

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?

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 3892100 posted 3:08 pm on Apr 15, 2009 (gmt 0)

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';
$flashwidth=330;
$flashheight=290;
$flashid='unique-id';
$variable_content = 'Hello CRUEL WORLD!';
var $msg ="
<html><head><title>Test</title></head><body>
<div id=\"flash-placeholder\">
This will be REPLACED by your Flash onload.
Use an image $flashwidth x $flashheight.
</div>
<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'); }
};
</script>
</body>
</html>
";
header("content-type:text/html\n\n");
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().

HelenDev

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3892100 posted 2:46 pm on Apr 17, 2009 (gmt 0)

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]

HelenDev

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3892100 posted 4:00 pm on Apr 17, 2009 (gmt 0)

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>

HelenDev

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3892100 posted 9:36 am on Apr 20, 2009 (gmt 0)

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 :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Flash and Shockwave
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved