calling php functions from flash
This tutorial assumes that you already have expirience with PHP or other programming languages. In this tutorial I will introduce PHP¡¯s dynamic function construction in combination with Actionscript. This combination allows you to execute various tasks in Flash with only one PHP script.
Assume you have a quiz application with PHP as middle layer with featues like login screen, questions, summary etc. You need to connect to a database to check the passwords, to load new questions etc. The usual way to develop this is to make sparate PHP scripts for login, etc. The more features you have in you flash app, the number of PHP scipts will grow that handle the special task.
Get ready
Well, ok, maybe you will be happy if your app uses only 3-5 scripts. But the other approach could be to use only one PHP script with separate functions that handle some tasks. The functions will be called from flash!
I will demonstrate a simple example and hope you get the idea:
Assume you have you script called myscript.php, the script contains the following function:
<?php
function say_hello_to_flash(){
echo ¡°&message=Hi, my name is string and I¡¯m coming from PHP, and you?¡±;
}
?>
On the other side you have your swf file residing in the same folder. Put a dynamic textfield on the stage and name it ¡°message¡±.
Now, what about PHP and the function? How does it work?
Well, before the final step I will try to explain to PHP¡¯s dynamic function names. Assume you have a usual PHP function:
<?
function scream(){
echo ¡°HELLO!!!!¡±;
}
?>
So if you wanted to call this function, you will normally write:
<? scream(); ?>
and you function will be executed.
But, take a look at this:
<? $my_func_name = ¡°scream¡±; $my_func_name(); ?>
What happened?
The last line of code was executed like
<? scream(); ?>
because $my_func_name has the value ¡°scream¡±!
OK, I understand, but what about flash?
Let¡¯s come back to our flash example. If you want to execute the function say_hello_to_flash(), you just have to construct a string with the value like the function name.
So in the first frame we write the following actionscript:
my_func_name = ¡°say_hello_to_flash¡±;
and then send the string to myscript.php:
loadVariablesNum(¡°myscript.php¡±, 0, ¡°GET¡±);
Now, PHP will receive the string my_func_name. To call the function you have to ¡°construct¡± the function call using the flash string:
$my_func_name();
That the basic idea, I used this method developing a quiz app. The function names were:
check_user, send_question, show_summary etc.
Cao
Mirza
| » Level Advanced |
Added: : 2002-09-17 Rating: 5.38 Votes: 89 Hits: 1337 |
| » Author |
| Mirza Hatipovic is freelance developer based in Bosnia and Hercegovina. He uses PHP, Flash, ColdFusion and XML to create dynamic web content. |
| » Download |
| Download the files used in this tutorial. |
| Download (0 kb) |
discuss this topic to forum
