Tutorial details:
Written by: Jesse Stratford
Time: 15 minutes
Difficulty Level: Intermediate
Requirements: Flash 4, 5, or higher
Topics Covered: How to send a request for data to a server side script and receive the answer without an intermediary data file.
Assumed knowledge: text fields, loadVariablesNUM(), some server side scripting language (I use PHP).

There's no example for this one, it's just theory. This tutorial was written back in the days when working with Flash and external data sources was much more difficult. With LoadVars Objects (see the Related Articles, below) it's now much easier. The tutorial is maintained for those working on older platforms, and because it may be of some use.

So you want to do some fancy processing of data using a server side script hey? Perhaps you want to call some data up out of a database? And how are your going to do it? You were going to create a temporary text file on your server weren't you! Don't deny it! I saw the look on your face! You were going to use loadVariables() to call your script, which grabs data from your database (or whatever) and then output it to a text file, which you would then load using loadVariables(). Well once you've read this tutorial you'll never use this crazy method again!

So basically this tutorial shows you how to make Flash ask for data from dynamic sources and retrieve it without the use of a dump file for loading purposes.

The secret to this is the standard formula for a loadable document. We all know if I want to load the string "Welcome Fred" into a Flash file I make a text document which looks something like this:

&message=Welcome Fred

I then load this file using


and I end up with a variable called message which contains my string. But what if I want to load a different person's name based on who has just logged in? If I run a big website which is totally built around databases and templates, using Flash can be a scary thing. But it's actually terribly easy to do this sort of thing.

All we use is the standard loadVariablesNum() command and a script. So you have your script which fetches the current user's full name from your database, base on their user name or something. Let's say they enter their username into a text field within our flash file and click "Enter". We then need to call up our database, telling it who has just logged in (by user name) and asking it for their full name (so we can print out our welcome message). This call takes the following form:

loadVariablesNum ("userToFullName.php", 0, "GET");

So we're calling 'userToFullName.php' and sending it (using GET) our user name variable. Now useToFullName has all it's database query code but we wont worry about that here. The important tag is just one short line and it looks like this:

print "&fullname=<strong>full name here</strong>";

Where full name here is the result you fetched from your database. In PHP we would store the fetched result in a variable, perhaps called $fullName so we'd have the following code:

// Database query code goes here.
 $fullName = // result of database query;
 print "&fullname=$fullName";

What this does is simply prints out the result in a format that Flash can understand. So if I entered my username, this script would return "&fullName=Jesse Stratford". This data is written out into the source of the page and because it's processed by the server before it is given to flash (PHP is a preprocessor, like JSP, ASP and others), Flash only ever sees the string "&fullName=Jesse Stratford", so it's happy. Upon receiving this data, Flash will create a new variable called fullName (if none already exists) and set it's value to "Jesse Stratford".

"Huh? A tutorial on that? Why bother?"

Because lots of people, and I'm not ashamed to admit that I was one of them until last week, would have make their PHP/ASP script output the sting "&fullName=Jesse Stratford" to a text file called temp.txt or something, then loaded in that text file to get their result. This way is heaps more efficient, just less well known.

For those of you who don't use PHP:

print() in PHP is just an output command ( like 'echo' etc.). All it does is output text to the file we're preprocessing, so this effect is achievable with ASP and all other sorts of preprocessors, you just need to substitute the code appropriately. (Please don't email me asking how to do it in ASP, because I don't know ASP! Post on the forums and someone who does will help you out :o)

Known Limitations:

The one main problem with this script is it's a real-time thing, so if you request a variable from your database or whatever, it's not going to load instantaneously. Doing it this way will improve load times compared to an temp data file method, but you will still have to allow for network congestion and the like. For this reason you may which to implement a loop which checks the value of the variable you expect to have returned, and waits until that variable has a value before trying to do anything with it. The counter tutorial deals with a loop like this, check it out.

Thanks to the kind folk in #PHP on Efnet who helped me figure this one out, and for teaching me PHP slowly over the last few months :o)

Jesse Stratford is the Co-Master of ActionScript.org and a freelance Flash developer and teacher. He is based in Australia and enjoys all things Flash.

NB: If you have comments or feedback please feel free to email me, but please do not email me Flash questions; the forums are provided for that purpose and you will get a faster answer by posting you question there.

If you have found this tutorial helpful, I hope that you will take 30 seconds to visit The Hunger Site where, with just one click you can make a free donation of food to a starving person in a third-world country. We do not benefit financially from this action; it is purely an act of charity.
This tutorial is protected by International Intellectual Property Rights laws and may not be reproduced or redistributed in full or part, without the prior written consent of the author. Unauthorized reproduction of this tutorial or its contents may result in prosecution. I've worked hard on this tutorial, please don't steal it.