Forum Moderators: open
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.
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.
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?
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().
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]
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>