faqts : Computers : Programming : Languages : PHP : Common Problems : Tips and Tricks

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

6 of 15 people (40%) answered Yes
Recently 4 of 10 people (40%) answered Yes

Entry

How do I pass variables from Flash4 to a PHP script and back again (PHP script in particular)?

Jul 3rd, 2001 18:34
Jo A,


I've worked it out myself.  After mucking around for ages I have got this answer, but it is not very elegant...
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;
   }
We 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.