如何使用externalInterface让Flash调用javascript来更新屏幕上的值?

发布于 2024-08-13 11:07:00 字数 1055 浏览 2 评论 0原文

我有一个嵌入 PHP 页面的 Flash 影片。 PHP 页面向用户显示一个值(他们已上传的图像数量)。当用户上传新图像时,我希望 PHP 页面上的值反映更改而不刷新页面。

该值是使用 MySQL 从数据库中检索的。所以这就是我到目前为止所做的 -

在我想要显示值的 PHP 页面上,我有一个 div

<div id="content_info"><script type="text/javascript" src="getInfo.php?group= <?php echo($groupid); ?> "></script></div>

这调用了一个外部 PHP 文件,该文件查询数据库并输出这样的结果

Header("content-type: application/x-javascript");
//do the query with PHP and get $number and then output
echo "document.write(\" (".$number.")\")";

当页面第一次加载正确的数字时显示在 div 中,所以一切正常。下一步是在值更改时调用某些内容来更新此 div 的内容。因此,我将在 flash 中设置 externalInterface 来调用 javascript 函数来执行此操作。

这就是我陷入困境的地方,我希望能够做这样的事情 -

function ReplaceContentInContainer(id) {
var container = document.getElementById(id);
container.innerHTML = getInfo.php?type=new&group= <?php echo($groupid) ?>;
}

并通过

ReplaceContentInContainer(content_info)

我意识到这不会起作用,但有人可以告诉我如何得到这个结果吗?

非常感谢

I have a Flash movie that is embeded in a PHP page. The PHP page displays a value to the user (the number of images they have uploaded). When the user uploads a new image I want the value on the PHP page to reflect the change without refreshing the page.

This value is retrieved from database using MySQL. So heres what Ive done so far -

On the PHP page where I want to show the value I have a div

<div id="content_info"><script type="text/javascript" src="getInfo.php?group= <?php echo($groupid); ?> "></script></div>

This calls an external PHP file that queries the database and outputs the result like this

Header("content-type: application/x-javascript");
//do the query with PHP and get $number and then output
echo "document.write(\" (".$number.")\")";

When the page loads for the first time the correct number shows in the div and so all works fine. The next step is to call something to update the contents of this div when the value changes. So I will set up externalInterface in flash to call a javascript function to do this.

This is where Im stuck, I want to be able to do something like this -

function ReplaceContentInContainer(id) {
var container = document.getElementById(id);
container.innerHTML = getInfo.php?type=new&group= <?php echo($groupid) ?>;
}

and call this by

ReplaceContentInContainer(content_info)

I realise this isnt going to work but can anyone show me how to get this result?

many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枕梦 2024-08-20 11:07:00

group= 仅当 PHP 创建页面时才会执行。您应该将该值存储在 javascript 的变量中。看看这是否有效。

<div id="scriptDiv">
    <script type="text/javascript">
    <!-- store the group id -->
        var groupID = <?php echo($groupid); ?>;
        function getGroupID()
        {
           return groupID;
        }
        function updateValue(value)
        {
           document.getElementById("content_info").innerHTML = value;
        }
    </script>
    <div id="content_info">
       <!-- for initial value -->
       <script type="text/javascript" 
            src="getInfo.php?group= <?php echo($groupid); ?> ">
       </script>
    </div>
</div>

现在您可以使用 flash 的 URLLoader

var ldr:URLLoader = new URLLoader();
var gid:String = ExternalInterface.call("getGroupID");
var req:URLRequest = new URLRequest("getInfo.php");
req.data = {type:"new", group:gid};
ldr.addEventListener(Event.COMPLETE, onLoad);
ldr.addEventListener(IOErrorEvent.IO_ERROR, onError);
ldr.load(req);

private function onLoad(e:Event):void
{
    var data:String = URLLoader(e.target).data;
    ExternalInterface.call("updateValue", data);
}
private function onError(e:IOErrorEvent):void
{
    trace("ioError");
}

group= <?php echo($groupid); ?> will be executed only when PHP creates the page. You should store that value inside a variable in the javascript. See if this works.

<div id="scriptDiv">
    <script type="text/javascript">
    <!-- store the group id -->
        var groupID = <?php echo($groupid); ?>;
        function getGroupID()
        {
           return groupID;
        }
        function updateValue(value)
        {
           document.getElementById("content_info").innerHTML = value;
        }
    </script>
    <div id="content_info">
       <!-- for initial value -->
       <script type="text/javascript" 
            src="getInfo.php?group= <?php echo($groupid); ?> ">
       </script>
    </div>
</div>

Now you can use flash's URLLoader:

var ldr:URLLoader = new URLLoader();
var gid:String = ExternalInterface.call("getGroupID");
var req:URLRequest = new URLRequest("getInfo.php");
req.data = {type:"new", group:gid};
ldr.addEventListener(Event.COMPLETE, onLoad);
ldr.addEventListener(IOErrorEvent.IO_ERROR, onError);
ldr.load(req);

private function onLoad(e:Event):void
{
    var data:String = URLLoader(e.target).data;
    ExternalInterface.call("updateValue", data);
}
private function onError(e:IOErrorEvent):void
{
    trace("ioError");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文