开发者

How to pass PHP variable as FlashVars via SWFObject

开发者 https://www.devze.com 2022-12-31 16:55 出处:网络
I am trying to take a PHP variable and pass it along to Flash via Flash vars. My end goal is to pass a string formatted as XML to Flash, but because I\'m struggling I\'ve stripped everything down to t

I am trying to take a PHP variable and pass it along to Flash via Flash vars. My end goal is to pass a string formatted as XML to Flash, but because I'm struggling I've stripped everything down to the basics. I'm just trying to pass a simple PHP string variable to Flash via FlashVars with SWFObject but something isn't right. The page won't load when I try to pass the variable inside of php tags, but it will load if I just pass a hard coded string. The basic structure of my page is that I have some PHP declared at the top like so:

PHP

<?php
     $test = "WTF";
?>

Some HTML (excluded here for simplicity sake) and then the JavaScript SWFObject Embed within my HTML:

    <script type="text/javascript" src="js/swfobject2.js"></script>
    <script type="text/javascript">
        // <![CDATA[
        var swfURL = "swfs/Init-Flash-PHP.swf";

        var flashvars = {};
            flashvars.theXML = <?php print $test ?>;

        var params = {};
            //params.menu = "false";
            params.scale = "showAll";
            params.bgcolor = "#000000";
            params.salign = "TL";
            //params.wmode = "transparent";
            params.allowFullScreen = "true";
            params.allowScriptAccess = "always";

        var attributes = {};
            attributes.id = "container";
            attributes.name = "container";


        swfobject.embedSWF(swfURL, "container", '100%', '100%', "9.0.246", "elements/swfs/expressinstall.swf", flashvars, params, attributes);


        // ]]>
</script>

And the bare essentials of the ActionScript 3 Code:

_paramObj = LoaderInfo(stage.loaderIn开发者_StackOverflowfo).parameters;
theText_txt.text = _paramObj['theXML'];

How do I pass a PHP variable using SWFObject and FlashVars?

Thanks.


Ugh, I needed to escape the Flash vars and it worked.

For anyone that's interested, this is what I needed to change

flashvars.theXML = <?php print $test ?>;

To this:

flashvars.theXML = escape('<?php print $test ?>');


escape() is not a foolproof way to escape (!)

Use encodeURIComponent() instead.

This is from point 9 at the FAQ: http://code.google.com/p/swfobject/wiki/faq

  1. How can I pass URIs or HTML code as a value using flashvars?

Special characters and the symbols = and & cannot directly be used inside flashvars values (the latter because they are used to stack the flashvars themselves).

You can workaround this issue by escaping these characters before passing them as flashvar values. An example:

encodeURIComponent("&trade") will become %26trade

The values will be available in your swf already unencoded, so no unescaping is needed inside your swf.

Note that encodeURIComponent is not available in all browsers, but is available in all of the common modern versions. If you need full backwards compatibility, you can use escape() instead, but note that escape() does not work well with double-byte characters (like Chinese).

You can also escape these characters manually by using:

* %3D instead of =
* %26 instead of &
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号