Sending Variables to PHP from Flash And Back again!

This is part 1 of a 3 part tutorial. The parts are:

Part 1: A simple setup that will send variables from a Flash movie to a PHP script and then that PHP script will print the variables out.

Part 2: Sending variables from a Flash Movie to a PHP script, and then using that PHP script to send an e-mail.

Part 3: Sending variables from Flash to a PHP script. Saving those variables to a text file, then calling them back into Flash to form a guestbook/shoutbox/tagboard within Flash.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

[size=4]Flash Player set-up[/size]
First you need to set up the input boxes. Create an Input box by using the “Text tool” In the text properties box, you must give the input box a name. It doesn’t matter what the name is, but you must remember that name because we are going to be using it in the PHP script in a few minutes.

In my file I made three input boxes. I named them “name”, “age”, and “eye”. Simple enough right? Ok, now we have to make the button to open up the PHP page. To open up a new page from Flash we use getURL(). The code that I used looks like this:

submit.onPress = function () {
      getURL ("getVars.php", "_blank", "POST");
}

getVars.php is the name of the PHP file that is our target.
_blank makes the button open up a new page.
POST is the method in which to transfer the variables. The other option is GET.

Thats it for the Flash part. :battery:

[size=4]PHP Page set-up[/size]
The PHP code is simple:

<?

// Recieving the variables.
$name = $_POST['name'];
$age = $_POST['age'];
$eye = $_POST['eye'];

// printing out the variables
print "Your name is ". $name . ".<br>";
print "You are ". $age ." years old.<br>";
print "You have ". $eye ." eyes.<br>";

?>

Now a quick explanation of the code:

The first three lines recieve the variables from the scripting using $_POST[]; This is necessary because the more recent versions of PHP have global variables turned off by default. On some hosts, your script will not work without using this method. You can name the variable whatever you like on the left of the equal sign ("="), but on the right, you must put the variable of your input text box.

The second 3 lines print out the strings. The extra periods (".") in there are used to connect the strings together. Its called the concatenation operator. For more about that go here: Click Here!

[edit]Sorry, I had to take out the script in action link because the server that the script was set up on was taken down.[/edit]

I am going to be re-writing this as a real tutorial to be posted on Kirupa’s main tutorial page. But I wrote this up while it was fresh in my mind. Hope it helps.

Cheers,
Jubs :crazy:

OK, I followed these instructions step by step and got nothing. Is there more to the scripts both AS and PHP than what is in the tutorial?


 
PHP->FLASH
<?php
//GaleryScoresText.php.
//In flash out
echo "GaleryScoresText=HELO!!!!!!";
?>
 
 
//fla
on (press)//button code
{
url="http://murmadillo.tut.su/submit_score.php";
my_load_vars=new LoadVars();
my_load_vars.load(url);
my_load_vars.onLoad = function() 
   {
   _root.memo_1.text=my_load_vars.HighScoresText;//Dynamic text
   }
 
}
 
 
FLASH->PHP
on (press)//button code
{
url="http://murmadillo.tut.su/submit_score.php";
//load_vars
var my_lv:LoadVars = new LoadVars();
var my_ret:LoadVars = new LoadVars();
 
my_lv.playerName = "HELLO";
my_lv.playerScore = "Alex Lexcuk";
my_lv.sendAndLoad(url, my_ret, "GET");
}
 
//end 
 
 
 
//php treason my_file.txt reason FLASH VAR
<?php
 
$result=$result.$_GET['playerName']." ";
$result=$result.$_GET['playerScore'];
 
echo $result;
 
$fh=fopen("my_file.txt","w");
 
fwrite($fh,$result);
 
fclose($fh);
 
//echo $buff; 
?>
 
 
//fla
on (press)//button code
{
url="http://murmadillo.tut.su/submit_score.php";
// load_vars
var my_lv:LoadVars = new LoadVars();
var my_ret:LoadVars = new LoadVars();
 
my_lv.playerName = "Alex";
my_lv.playerScore = "This Mega GOOD";
my_lv.sendAndLoad(url, my_ret, "GET");
_root.memo_1.text = "Send...";
my_ret.onLoad = function() 
   {
    _root.memo_1.text ="Well done.";
    }
 
 
}
 
 
<?php
session_start();
$result_name=strip_tags($_GET['playerName']);//chaccer killer
$result_score=strip_tags($_GET['playerScore']);//crack killer 
//that time?
$result_time = date("d.m.Y H:i"); 
//example 07.09.2008 08:50 
 
 
//create array
$split_data=$result_name.'&'.$result_score.'&'.$result_time."
";
echo $result;
 
$fh=fopen("my_file.txt","a+");//add in end of file
 
fwrite($fh,$split_data);//write file
//echo $buff; //in flash out
fclose($fh);//close file my_file.txt
 
?>
 

It’s been proven to be very good and help many members. If you notice the post date “03-16-2003, 08:42 PM” maybe you can understand why there are no files.