Skip to main content
Known Participant
March 23, 2012
Question

Passing variables from php to flash and the opposite

  • March 23, 2012
  • 1 reply
  • 5567 views

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!

This topic has been closed for replies.

1 reply

tripleA999
Known Participant
March 23, 2012
Known Participant
March 29, 2012

thanks but thats for as2 and my project is in as3. Doesn't anyone know how to do this?please!

_spoboyle
Inspiring
March 29, 2012

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++;

}

?>