Flash to PHP Comunication

Flash, not being server-side, cannot itself retrieve nor send information to a database. That sucks, but don't leave this website yet because there's still a way:) We can use a sever-side script language known as PHP. It can easily interact with databases as well as Flash. In this tuturial I'm going to make an attept to explain how you can send and receive information from PHP (which retrieved a record from a database beforehand).

To complete this tutorial you need to have some hosting that supports PHP  (I guess this requirement is met by 99,99 host companies) or you can also have PHP installed locally, but if I were you I'd stick to the first option.

Sending variables from Flash to PHP

There are actually a number of ways to do that. The simpliest one is probably sending GET data in querystring. Not sure what that means? Let me give you an example, have you ever seen an url like: http://www.somewebsite123.com?user=324&&ref=99

 

In plain English, we're sending the page under the url somewebsite123.com, variables  user=324 and ref=99. 

The trick here is to use the well-known command getURL(url,method) to open an url inside the browser. In our case we'd write: getURL("http://www.somewebsite123.com?user=324&&ref=99","_self");

Double && is obviously used to separate two variables.

 

So, now PHP code, if we were to capture these values we'd do this:

<?php

$ref = $_GET['ref'];

$user = $_GET['user'];

?>

At this point we can do with these variables whatever we want, like inserting them into a database.

OK, so this opens a new window  or loads the page inside an opened window. What if we want to do it without letting the user know about it? For example, we might want users to be able to send e-mails directly from our Flash website.

Sending variables (advanced)

var myVars = new LoadVars ();
myVars.email = input_box.email.text;
myVars.name = input_box.name.text;
myVars.sendAndLoad("http://www.yowebsite94.com",newsVars);

The code above creates a new LoadVars instance, adds a few values to the LoadVars object and then sends the variables to a script called at http://www.yowebsite94.com. The variables are sent as POST data.

And here's a cool thing: this script actually not only sends variables but also receives response variables! To test that do this:

Create an empty PHP file.

Type in the following code:

$name = $_POST['name'];

$email = $_POST['email'];

$responseText = "Hi $name, your email is $email";

echo "responsetext=$responseText";

Yes, that's the only format that Flash understands. Each variable must be output in this way, using && to seperate one from another.

 

If we return to Flash, we might output this text using the trace function.

 

What if we want to only load variables?

myData = new LoadVars();
myData.onLoad = function(success) {
    if (success) {
       myVar = this.myvar;
    } else {
        trace("Error loading!");
    }

};
myData.load("http://www.mycoollittlewebsite.com/script.php");

The code does the exact same things, however this time we've got an line that keeps an eye on the process of loading the external variables. It literally tells Flash to wait until the variables are loaded into the SWF file. Once that happens, it points to the onLoad function that captures myVar variable. The variable must be output in the PHP file as: myvar=some value.

 

Average: 4.1 (7 votes)