Basic PHP and Macromedia Flash Interaction Part 1


posted: 18-10-05
author: PrimeVector
website: http://www.free-webmaster-resource.com
views: 18048
comments: 16
total pages: 2
current page: 1
page jump: 1 | 2

download source files


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.



Amazing Webmaster Widgets





next page
page jump: 1 | 2
jump to comments

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!



Latest News

Amazing Webmaster Tool

If you run a MySpace or teens related site with visitors interested in GLITTERS you will need this great tool: Webmaster Glitter Tool It will generate backlinks at an amazing pace!

 

Useful Click To Clipboard Script

This is a nice little copy to clipboard script. You can find it here!

 

Google Pages

Create pages on google quick and easy with Google Pages!

 

My Funny Pictures Site

Have fun on this funny pictutures site i made, i hope youll enjoy it :) PixHumor

 

IS YOUR CHILD SAFE???

With this great program you can make sure your child will be a lot safer while browsing the Internet.

 

New Site

I created a fun site for MSN Messenger Stuff called MSN Freak. You can find the site here: Free Winks @ MSN Freak

 

Useful free unlocker tool

This tool is a very useful tool that runs in the background. Whenever you try to for example copy a folder and explorer sceams at you it wont do that, the barrier is lifted and you can continue to do what you wanted too. Download it here.

 

GOOGLE worldwide: Languages and countries

Useful page showing all the Google Search Engines of all countries. Check it out here.

 

Google Trends

Google trends enables us to see where people want certain things the most. You can enter a keyword and find out where people search for that most often! Check Google Trends

 

Google PageRank Checker

I personally enjoy this Google pagerank checker a lot: Google PageRank Checker

 

New tutorials

I added a new basic Flash tutorial showing how to create a simple motion tween. You can find it here: Basic Flash Motion Tween

 

2 new Flash tutorials

I added 2 new Flash tutorials to the tutorial selection. On deals with the if else conditional statement and the other deals with setting up a bit of color with actionscript. You can checkout the tutorials here: Actionscript and colors and If Else Conditionals.

 

Oh My!

Its mad times overhere, im helping promote and have created a few sites which has taken quite some time from me. site1: http://www.flashgamefreak.com, site2 http://www.freewinks.net, site3 http://www.smilieworx.com and more are to come this and next week. This has taken its toll on my time to write tutorials. I will make up for that as soon as possible. Thanks for your understanding.

 

Basic Flash Tutorials

I added 3 new basic Flash tutorials to the tutorial list. Dealing with layers, What are functions and one about The Registration Point.

 

A bit of a delay

I have had a very crazy time around here the last month, because of this some tutorials have not been written yet. This is a list of tutorial i still need to write and you can expect them in the very near future.

 

Advertise on FWR

If you want to advertise here then please contact me here. We can make a nice deal and you can get stats on them how often you would like.

 

2 new tutorials

2 new web design related tutorials were added ... more Flash tutorials are on their way but its a bit mad around here these last days ;)

 

View All News



In Focus

Play Frogger Now!
Play frogger with 1 click download of gamevance!
Play Airstrike 1941 online for free!
Play Airstrike 1941 online for free!
Free Auto Insurance Quotes!
Saving money with InsureMyCar4Less is simple. After you fill out our quick online form, you get free auto insurance quotes from the best providers in the industry. They compete for your business, and you save money.
Play World War 2 Commando Free!
Play World War 2 Commando online for free!
Play Aztec Quest online for free!
Play Aztec Quest online for free!
Get a Free Auto Insurance Quote
Free quotes from top insurance carriers!
Do you shop at Home Depot?
Get a ton of Home Depot coupons and gift card offers! Also if your looking to improve you home this is the place to be!
Make cash with Penny Stocks!
Make cash with Penny Stocks!
Play Swat 2 Now
Play the swat 2 game and shoot those bad guys!
Play Bubble Boomers
Pop the bubbles of the like colors for this very fun and addictive game for FREE!!!
Customize your Facebook with a theme
Customize the colors and music on your facebook profile page today!
Print Coupons from Home!
Print Grocery Coupons from your house! Instant Savings!
Get free Auto Insurance Quotes!
Saving money with InsureMyCar4Less is simple. After you fill out our quick online form, you get free auto insurance quotes from the best providers in the industry. They compete for your business, and you save money.
Stop identity theft in its tracks!
Protect My ID makes it easy for you!
Save up to 70% on auto insurance!
Do you qualify for 60-70% off your auto insurance rates? Find out right now! Request quotes 24 hours a day with our secure online form. It's super fast and easy. See what your rate is absolutely free!
Play Streetfighter Now!!!
Get all the hottest new games and all the old school games in one place!
Play Pac Man right here!
Get all the hottest new games and all the old school games in one place!
Play the old school Baseball Stars
Get all the hottest new games and all the old school games in one place!
Dont get your identity stolen!
we offer multiple layers of protection and are backed by experian. Start with only $1!
Share your Opinion, win $50,000?
Share your Opinion, win $50,000? Couldn't get any easier! It's free and fun!

Cool Stuff