Actionscript to Server Comunication without Flex or a PHP daemon.
This article has been added to your 'Articles to Read' list.

Actionscript and Javascript
Joseph Tveter
PHP, JavaScript and ActionScript can be friends. The net seems short on information on how to do so. So I hope return what I have learned.
View all articles by Joseph TveterThe 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'.
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.
Spread The Word
Attachments
6 Responses to "Actionscript to Server Comunication without Flex or a PHP daemon." 
|
said this on 24 May 2010 8:27:35 AM CDT
Nice job; pretty cool lit
|
|
said this on 04 Jun 2010 12:08:46 PM CDT
Thanks,
It was a fix |
|
said this on 11 Jun 2010 2:13:44 PM CDT
Using AJAX can make this
|
|
said this on 12 Jun 2010 12:31:28 AM CDT
Thanks, I will look into
|
|
said this on 18 Jun 2010 2:01:35 PM CDT
this can be done with dir
|
|
said this on 15 Jul 2010 8:12:52 AM CDT
I agree. And it would be
|



Author/Admin)