Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Passing variables from php to flash and the opposite

New Here ,
Mar 23, 2012 Mar 23, 2012

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!

TOPICS
ActionScript
5.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 23, 2012 Mar 23, 2012
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 29, 2012 Mar 29, 2012

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 29, 2012 Mar 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++;

}

?>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 29, 2012 Mar 29, 2012

thank you very much. Could you explane me what URLLoader and URLRequest do??? Its the only think i havent understand

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 29, 2012 Mar 29, 2012

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 30, 2012 Mar 30, 2012

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!!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 05, 2012 Apr 05, 2012

anyone here?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 30, 2017 Jul 30, 2017
LATEST

I just want to say that your code is beautifully written and easy to understand.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines