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 codebasicly 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 dataPHP 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 thisPHP 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 & phpwill write up one on gaining multiple lines of data and splitting it in flash as3