2007.04.12
category
comments

flashとjavascriptとphpでやりとりするには

flashとjavascriptのやり取りは「ExternalInterface」を使う。

JAVASCRIPT:
function getJavascript()
{
   var obj = new Object();
   obj.infoTitle = document.URL;
   obj.infoUrl = document.title;
}

ACTIONSCRIPT:
import flash.external.*;

if(ExternalInterface.available)
{
   infoObj = ExternalInterface.call("getJavascript");
   title.text = infoObj.infoTitle;
   url.text = infoObj.infoUrl;
}
else
{
   title.text = "ExternalInterfaceを利用することが出来ません";
}

javascriptからのデータがinfo.objの中に格納される。

続いて、flashとphpのやり取りは「sendAndLoad」を使う。

PHP:
<?php
   $message= $_POST["message"];
   echo "responce=".$message."をflashに返します";
?>

ACTIONSCRIPT:
//-----[送受信オブジェクトのインスタンス化]
var loadLV:LoadVars = new LoadVars();
var sendLV:LoadVars = new LoadVars();

//-----[渡したい変数名と値を定義]
sendLV.message = "hoge";

//-----[コールバック関数を定義]
loadLV.onLoad = function(success)
{
   if (success == true)
   {
      trace(loadLV.responce);
   }
   else
   {
      trace("通信エラー");   //hogeをflashに返します"
   }
}

//-----[sendAndLoadメソッドを使ってデータの送受信を行う]
sendLV.sendAndLoad("phpファイルのパス", loadLV, "POST");

Post a comment




Comment

Trackbacks

この記事のコメント・トラックバックRSS