This is a live example of: basic_PHP_to_Flash_interaction.swf
advice
Readers that dont have Flash 8 installed wont be able to view the source files. They were created with Flash 8.
You can buy
Macromedia Flash Pro 8
here, or just download a trial first.
This is the latest and best version of Flash and we can do nothing else but recommend it to everybody because we absolutely love it.
This new edition opens up a lot more of the magic that was already there, and it certainly is a new fresh
wind for us Flash developers. Its a great investment for the future.
Sorry for not having the source files available in all versions, if demand is high i will try to add more types of source files where needed?
What am i going to learn in this tutorial ?
You will learn to use the LoadVars object to retrieve a variable we will setup in a
PHP file and display its content in a dynamic textbox in Macromedia Flash. You can use this tutorial
as a first introduction into using PHP with Flash Actionscript.
To make this tutorial
work for you too you must have access to a PHP enabled webserver.
Most hosts, even the free ones support PHP and if you really want to start using PHP you really must have access to it!
If you are a dutch user, you can get incredible speeds at lycos, have a look
here.
Onward to the Flash stage
Download the source files of this tutorial and have a good look at the setup of the file.
Its a really simple small file with just one dynamic textbox named 'displayTxt' and some
describing static text. There are only 3 layers, 1 for the static text, 1 for the codes and 1
for the dynamic text. Select the dynamic textbox and have a look at its name in its properties box.
It reads 'displayTxt' and its important to never forget that you will have to name the object you want
to address with your actionscript codes! In this case we want to fill this textbox via actionscript so we give
it a name here and use that name in our codes to address this object. (the textbox)
Now select the code layer and have a first look at it. Its not a lot of code so dont be afraid of it.
The best thing you can do at this point is open a new file and re-create the setup i created in this
swf but leave the code blank. Then start to input the code line by line while reading the describing info
per line of code. When you have entered all the code dont hurry to press that control-enter yet because
we have to do a little bit of PHP work first. Below is the code with descriptions and on the nex page its
also shown again without comments.
Lets begin the Actionscript part
First we have to setup an instance of the LoadVars Object.
We do this with the 'new' keyword. We give it as name 'receiver_lv'.
The variable we will setup in the PHP file a bit later will be appended
to our receiver_lv loadVars instance. We can easilly grab its
content and make it visible and for the world to see. In our case this means
we will show the content of that variable in a textbox on stage.
var receiver_lv:LoadVars = new LoadVars();
This is where some of the magic starts. You can see the onLoad part
as a little engine where you can do a lot of things, and mainly that
means getting the data you had setup in the PHP file and display it.
If there is anything wrong with the loadVars connection the variable 'ok' will
be set to false and that will cause and error message to be displayed in our textbox.
receiver_lv.onLoad = function(ok){
So here we say: if variable 'ok' is set to 'true', execute the code between the {....},
and if its not set to true execute the code in the else{.....} part.
if(ok){
Now we will actually grab our data and show it in a textbox named 'displayTxt'.
I said earlier that the variable we setup in PHP would be appended to our
LoadVars instance. In our case that instance is called 'receiver_lv'. Here you
can see that in effect if you inspect this part of the code
receiver_lv.myPhpVariable_1. The . between receiver_lv and myPhpVariable_1
means that 'myPhpVariable_1' is a variable in 'receiver_lv'. So with the line
receiver_lv.myPhpVariable_1 we get the content of myPhpVariable_1
which was is a variable we will create in the PHP file.
displayTxt.text = receiver_lv.myPhpVariable_1;
This code will be execute if there was an error in connecting to the PHP file.
}else{
display an error message in our displayTxt.
displayTxt.text = "There was an error!!";
}
}
At this point the only thing in Flash we have to do is setup the actual connection to the PHP file.
We will do it thistime with the 'load' command, because we only want to retrieve 1 little lonely variable.
receiver_lv.load("get_my_php_data.php");
On the next page you will first find the code we created in this page but without any comments,
and you will also setup the PHP file.
Yes, this has been a ridiculous search for me. This is a very simple operation and you explained it succinctly and well. Thanks
30-01-07:Steponfrog
That, my very fine friend, is absolutely brilliant! I couldn't have asked for a much simpler tutorial on the subject. But, one important point to make is that on all the tutorial sites I've visited searching for PHP & Actionscript none actually explain whether variables are passed from PHP to Flash with any intervening bits, or whether PHP actually PRINT() the variables to the screen before Flash picks them up... this may be trivial to experts or those with Actionscript & PHP knowledge, but to novices just learning about all this stuff PRINT() means send something to a device -- usually a screen, or printer.
Imagine the novice's horror when they believe their important (not to be seen by anyone at all) variables are printed to the screen for all the world to see... 8-o
Hope you can amend the PRINT() explanation...
Step :-D
Comments
16 comment(s) found in 3 pages
viewing comment(s): 1 - 6
page: 1
1 | 2 | 3 |
05-09-07:guest
dfghjk kk
12-05-07:guest
17-04-07:marcelo Caneschi
teste de mensagem Flash Perfection
30-03-07:ya
hi indeed
01-02-07:thankful
Yes, this has been a ridiculous search for me. This is a very simple operation and you explained it succinctly and well. Thanks
30-01-07:Steponfrog
That, my very fine friend, is absolutely brilliant! I couldn't have asked for a much simpler tutorial on the subject. But, one important point to make is that on all the tutorial sites I've visited searching for PHP & Actionscript none actually explain whether variables are passed from PHP to Flash with any intervening bits, or whether PHP actually PRINT() the variables to the screen before Flash picks them up... this may be trivial to experts or those with Actionscript & PHP knowledge, but to novices just learning about all this stuff PRINT() means send something to a device -- usually a screen, or printer. Imagine the novice's horror when they believe their important (not to be seen by anyone at all) variables are printed to the screen for all the world to see... 8-o Hope you can amend the PRINT() explanation... Step :-D
- Press here to view all available smilies!