MMOCC the isometric world scene, 2006–now
Archive. Archived from the original MMOCC Forum (2006–2010), recovered from the Internet Archive. The forum is read-only history now — the scene talks on Discord. If you posted here and want something removed, get in touch.

ActionScript & PHP (Sending Variables/Data)

10 posts · 2011-01-19 · MyBB era
01-19-2011 02:05 PM #1
Hello, this is a tutorial on how to send data from actionscript to php.
In this tutorial you will learn how to send data, handle the data via php,mysql then sending new or updated data to flash as3
firstly import these class's once you have created a fresh as3 file
about these imports
PHP Code:
//Handles URL errors
import flash.events.IOErrorEvent;
//Way you want to send data witch is "POST"
import flash.net.URLRequestMethod;
//Data variables
import flash.net.URLVariables;
//Event Class - should know this one
import flash.events.Event;
//Loads Url
import flash.net.URLLoader;
//files name on witch is getting loaded (my/file.php) etc
import flash.net.URLRequest; 
now to send a request or data to php add this code
basicly ill show you a login code for this tutorial
PHP Code:
private function tutorialSendInfomation(myusername:String, mypassword:String):void
{
//Create some stings to hold infomation can be private or public
var myname:String;
var mypass:String;
//assign function strings to created strings if you wish
myname = myusername;
mypass = mypassword;
//assign the request url/destination    
phpRequest = new URLRequest("my/file.php");
phpLoader = new URLLoader();
//set up the variables var1, var2 for data sending etc    
vars = new URLVariables();
vars.var1 = myname;
vars.var2 = mypass;
//assign data as vars & assign request method as post    
phpRequest.data = vars;
phpRequest.method = URLRequestMethod.POST;
//add some event listeners this ones for any Loading or Data  errors
phpLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
//this one is once data is send it adds a event listener for the incomming result
phpLoader.addEventListener(Event.COMPLETE, tutorialReceiveInfomation);
//send the request
phpLoader.load(phpRequest);

This function is for receiving the handled data
PHP Code:
private function tutorialReceiveInfomation(event:Event) 
{   
phpResultVars = new URLVariables();
phpResultVars.decode(event.currentTarget.data);
            
trace("Login Data:" + phpResultVars);
//in this tutorial im only showing single line incomming data next tut will show how to split data etc so the user & pass was correct it will send back (correct) if was incorrect sends back (wrong) get the drift
if(phpResultVars.resultData == "correct")
{   
trace("Login Granted");
//handle some logn data etc
}
else if(phpResultVars.resultData == "wrong")
{   
trace("Login Failed");
//displays a incorrect password message etc
}

now the the file.php code it will look something like this
PHP Code:
<?php
//include a database connector if mysql
include 'mydb.php';
//get the vars that was send from flash
$receivedUsername = $_POST["var1"];
$receivedPassword = $_POST["var2"];
//basic example code syntax could be wrong lmao
$query = "select user_id from mytable where username='$receivedUsername' AND password='$receivedPassword'";
//get the result
$result = mysql_query($query);

    
     if (mysql_num_rows($result) > 0)
    {        
         //just a var
          $correct = "correct";    
        echo "resultData=" . $correct;
    }
    else if (mysql_num_rows($result) == 0)
    {   
         //another var
          $wrong = "wrong";
        echo "resultData=" . $wrong;
    }

?>
and thats basicly how to send & receive data using as3 & php
will write up one on gaining multiple lines of data and splitting it in flash as3
01-19-2011 02:08 PM #2
I'm definitely taking some notes from your post because this would be very handy for my MMO.

01-19-2011 02:20 PM #3
it comes handy if you wish to do some basic loading & saving datas i dnt advise totally coding a mmo using post request but its good for some requests you can do in a mmo

01-19-2011 04:16 PM #4
Nice tut, though I would advise you to at least use addslashes() around your post vars.

01-19-2011 04:54 PM #5
it was just a quick write up.. sorry for any mistakes or syntax erros you occur but its all simply fixed if you know as3 but it doesnt matter about slashes its only a string... for storing temp infomation

01-19-2011 08:13 PM #6
use code tags.

04-27-2011 03:26 AM #7
@Jason: You'll mean htmlentities() and mysql_real_escape_string().

04-27-2011 04:09 AM #8
(04-27-2011 03:26 AM)Raoul Wrote:  @Jason: You'll mean htmlentities() and mysql_real_escape_string().

Addslashes at the least, but preferably mysql_real_escape_string()

04-29-2011 06:07 AM #9
Thanks men , and use public , not var because some php vers don't support Vars on classes

05-01-2011 04:16 AM #10
Thanks for this

Recovered from /printthread.php?tid=18324 · captured 2011.