Lab: SWFObject
Passing querystrings to a SWF using PHP
Someone once asked about passing querystring parameters to a SWF on page load. This is a pretty simple task in PHP, and ASP and other server-side languages will use similar techniques.
Getting the querystring:
foreach($_GET as $variable => $value) {
// do something with $variable and $value
}
SWFObject 2.0 has both static and dynamic publishing options, each requiring a different approach to flashvars.
Static publishing
The static publishing method requires adding flashvars as a <param> element in the object. Here's one way to do it (I'm sure there are other, potentially better ways!). Note the dual <param> element; since the outer <object> targets Internet Explorer and the inner <object> targets all other browsers, each <object> requires its own <param> element.
<object id="flashcontent" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="550" height="400">
<param name="movie" value="/lab/_common/sample.swf" />
<?php
$flashvars = "";
foreach($_GET as $variable => $value) {
$flashvars .= $variable . "=" . $value . "&";
}
//Get rid of the last ampersand
$flashvars = rtrim($flashvars, "&");
?>
<param name="flashvars" value="<?php echo $flashvars ?>" />
<!--[if !IE]>-->
<object type="application/x-shockwave-flash" data="/lab/_common/sample.swf" width="550" height="400">
<param name="flashvars" value="<?php echo $flashvars ?>" />
<!--<![endif]-->
<p>Please update your Flash Player</p>
<!--[if !IE]>-->
</object>
<!--<![endif]-->
</object>
Dynamic publishing
The dynamic publishing method allows you to use a JavaScript object to pass the variables.
<script type="text/javascript">
var flashvars = {};
<?php
foreach($_GET as $variable => $value) {
echo "flashvars." . $variable . " = " . "'" . $value . "'; \n";
}
?>
var params = {};
var attributes = {
id: "flashVarsExample",
name: "flashVarsExample"
};
swfobject.embedSWF("/lab/_common/sample.swf", "flashcontent", "550", "400", "7", "/lab/_common/expressinstall.swf", flashvars, params, attributes);
</script>
Working examples: static publishing | dynamic publishing (tested in Firefox 2, IE 6, and Safari 3.1 Windows).