The foundation has been set.  We have a path from the database to a JavaScript function. Actionscript 3 has made calling JavaScript functions very easy but there are a few things to make ready before you can call.

Flash sometimes will load faster then JavaScript throwing an error if you call JavaScript before its done loading.  There are a couple ways around this but the one I prefer is to start a timer that will not let the program progress until JavaScript is loaded.

var timer:Timer = new Timer(50, 0);

            timer.start();
            timer.addEventListener(TimerEvent.TIMER, JSReadyFun)
           
            function JSReadyFun(event:TimerEvent):void
            {
                JSReady = ExternalInterface.call("JSReady");
                EF.txt.appendText("JSReady-" + JSReady +"\n");
                if (JSReady == true)
                {
                    timer.stop();
                    EF.txt.appendText("Javascript Ready! \n");
                   
                    //When JavaScript is ready you can begin calling functions to display your dynamic content.

                    Status = ExternalInterface.call("GetStatus");
                    Status = Status.substr(0, 5);
                    Go();
                }
                else
                {
                    EF.txt.appendText("Javascript Not Ready. \n");
                }
            }
 

Here is the function in JavaScript that it is calling to see if it is ready.

//Function to check if Javascript is ready
function JSReady()
{
     Ready = true;
     return Ready;
}


Once Flash is going the user can interact with it and use the javascript functions to query the database. The button is asking for all the wedding pictures by way of these functions.

function LoadPics(e:MouseEvent):void
            {
                //Excutes Form by way of javascript
                ExternalInterface.call("Submit", "Wedin");
            }


The ExternalInterface.call to Submit in JavaScript executes the form Main.

function Submit(Status)
{
      document.Main.Status.value = Status;

      document.Main.submit();
}
 


Now the Page will reload with a form variable Status = 'Wedin'.  At the server the PHP captures the form data and places it in the string '$Status'.

if(isset($_POST['Status']) and trim($_POST['Status']) != "" and trim($_POST['Status']) != "undefined" )
{
     $Status = trim($_POST['Status']);
}
else
{
         $Status = 'Start';
}


The switch on $Status executes the MySQL query in the GetGallery function.  You could save code by building the query data to a php string instead of an array, but i left it in just in case anyone wanted to see how to convert a php array to a JavaScript array.

switch($Status)
{
     case Start:
     break;

     case Wedin:
          $WeddingArr = GetGallery('Wedding');

          //Array prep for JavaScript because you can not pass arrays from PHP but you can pass strings that can be turned into arrays.
          foreach ($WeddingArr as $key => $value)
          {
               if ($key < (count($WeddingArr)-1))
               {
                    $script = $script . "'" . $value . "',";
               }
               else
               {
                    $script = $script . "'" . $value . "'";
               }
          }

          //Now we build the Array in JavaScript and place it in a callable function.

          echo "
          <script language='javascript'>
               function GetWed()
               {
                    var Wed = [$script];

                    return Wed;
               }
          </script>
          "
;

}


function GetGallery($AlbumName)
{
     //Establish The Record Count
     $cnt = 0;

     //Establish the query to the database and pull the result.
     $query = "SELECT * FROM photo WHERE AlbumName = '$AlbumName' ";
     $result = mysql_query($query) or die ('Could not execute');

     if($result)
     {
          //If there is a result build it into an array.
          while($row = mysql_fetch_array($result))
          {
               $returnMe[$cnt] = $row['folder'].$row['ImgName'];
               $cnt ++;
          }
     }
     return $returnMe;
}




Calling a function and retrieving an array is no differant then returning a string, int, or boolean.  Here is the capture of the array by Actionscript.

var WedPicList:Array = new Array;
WedPicList = ExternalInterface.call("GetWed");


There and back again passing PHP to JavaScript to Actionscript to JavaScript to PHP.