faqts : Computers : Programming : Languages : PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

11 of 18 people (61%) answered Yes
Recently 6 of 10 people (60%) answered Yes


Is it possible to run a php script from an .swf and then return a few variables back to the .swf.

Feb 22nd, 2008 03:25
dman, Jo A, Ryan Godfrey, http://www.ttnr.org

I wanted to know this myself, but nobody seemed to know.  After mucking
around for ages I have got this answer, but it is not very 
I'm presuming that you have the flash stuff down pat (remember to get
the level correct - usually 0 not 1, and remember to enable FS 
commands when publishing).  I used flash 4 I will check this with flash
5 later.
The variables get passed to php as $HTTP_POST_VARS (or GET).
I'm not sure if this is an associative array, but you can do associative
array type things to it.
To turn it into php variables:
use reset ($HTTP_POST_VARS) to point to the start of the array.
Make a while loop with (list ($key, $value) = each ($HTTP_POST_VARS) )
as the condition.
Within the loop check the name of any variable you are interested in
against $key, and then create a php variable with the same name 
and give it the value of the key.
For example:
   if ($key == "myVariable")
      $myVariable = $value;
You can write this out for a whole bunch of variable names, I used
switch to do it slightly more elegantly.
We now have the flash variables transformed into php variables... but
what about sending them back?
Well this is crazy but we simply print them out in urlencode format.
For instance: 
print '&myVariable=' . $myVariable;
I haven't checked it yet, but it is probably a good idea to urlencode
the php variable (in case we put, say, a space into the value of 
$myVariable by mistake):
print '&myVariable=' . urlencode ($myVariable);
I don't know why, but if you print out such a line twice, the first line
is ignored.
For instance:
print '&myVariable='peanut';
print '&myVariable='cashew';
Doesn't create an error, instead it tells flash that myVariable has
"cashew" as its value.