Passing variables from php to flash and the opposite
Copy link to clipboard
Copied
Hi guys, im trying weeks now to solve this problem but nothing yet
If someone could just tell me how to pass variables from flash to php and the opposite i would be thankful!!! Please help!
Copy link to clipboard
Copied
this will help you
Copy link to clipboard
Copied
thanks but thats for as2 and my project is in as3. Doesn't anyone know how to do this?please!
Copy link to clipboard
Copied
I have recently had to learn this, so this may not be the best way but it worked for me
I suggest looking at the code below stripping out everything you don't need (e.g. the databse stuff) and just get a simple string going back and forward
have a go and post any problems here and I'll try and help
in flash i have
private function getBalanceAndXP():void
{
var request:URLRequest = new URLRequest("utils.php");
request.method = URLRequestMethod.POST;
var variables:URLVariables = new URLVariables();
variables.func = "getBalance";
variables.fbid = userID;
request.data = variables;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, onBalanceComplete);
loader.load(request);
}
private function onBalanceComplete(e:Event):void
{
var loader:URLLoader = e.target as URLLoader;
loader.removeEventListener(Event.COMPLETE, onBalanceComplete);
var variables:URLVariables = new URLVariables(loader.data);
_balance = parseInt(variables.balance); // class variable
_experience = parseInt(variables.experience); // class variable
}
public function setBalanceAndXP(balance:int, experience:int):void
{
_balance = balance;
_experience = experience;
var request:URLRequest = new URLRequest("utils.php");
request.method = URLRequestMethod.POST;
var variables:URLVariables = new URLVariables();
variables.func = "setBalance";
variables.fbid = userID;
variables.balance = _balance;
variables.experience = _experience;
request.data = variables;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(request);
}
and then I have my php file
<?php
$func = $_POST["func"];
$fbid = $_POST["fbid"];
$balance = $_POST["balance"];
$experience = $_POST["experience"];
$numVariables = 0;
$link = mysql_connect("localhost","username","password");
mysql_select_db("databaseName");
if ($func == "getBalance")
{
getBalance($fbid);
}
else if ($func == "setBalance")
{
setBalance($fbid, $balance, $experience);
}
mysql_close($link);
function getBalance($fbid)
{
$query = "SELECT balance, experience FROM tableName WHERE fbid = '".$fbid."'";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
writeVariable("balance", $row[0]);
writeVariable("experience", $row[1]);
}
function setBalance($fbid, $balance, $experience)
{
$query = "UPDATE tableName SET balance = ".$balance.", experience = ".$experience." WHERE fbid ='".$fbid."'";
mysql_query($query);
}
function writeVariable( $name, $value )
{
global $numVariables;
if ( $numVariables > 0 )
{
echo "&";
}
echo $name . "=" . urlencode($value);
$numVariables++;
}
?>
Copy link to clipboard
Copied
thank you very much. Could you explane me what URLLoader and URLRequest do??? Its the only think i havent understand
Copy link to clipboard
Copied
URLLoader is a class used to load URLRequests
from the API
The URLLoader class downloads data from a URL as text, binary data, or URL-encoded variables. It is useful for downloading text files, XML, or other information to be used in a dynamic, data-driven application.
in our case we are downloading url-encoded variables
URLRequest represents the location of the data you are trying to access in this case a php script
from the API
The URLRequest class captures all of the information in a single HTTP request. URLRequest objects are passed to the load()
methods of the Loader, URLStream, and URLLoader classes, and to other loading operations, to initiate URL downloads. They are also passed to the upload()
and download()
methods of the FileReference class.
Copy link to clipboard
Copied
Ok thank you very much man! Is it easy to explane me your codes line by line? You can make an pdf file if you want and send it to my mail [removed] Thank you again!!!
Copy link to clipboard
Copied
anyone here?
Copy link to clipboard
Copied
I just want to say that your code is beautifully written and easy to understand.

