ActionScript 3 to Post to PHP and Retrieve Variables with a Simple AS3 and PHP Login
Have you ever wanted to post variables from AS3 to a PHP and get data back from the PHP? This tutorial will show you how to easily take ActionScript 3 variables and post the to PHP. We will create a simple login with AS3 and PHP.
In our ActionScript we will be using urlRequest and URLLoader to send data to a PHP file and retrieve information back into the ActionScript.
In our urlRequest we will first determine the location of our PHP file and then set any variables that we want to send to the PHP. We will add all of our variables to a URLVariables() object and add that to our urlRequest.
We then add our request to the loader and add an event listener to fire on completion. There we will grab our variables that were sent from our PHP file.
|
1 2 3 4 5 6 7 8 |
/*recommended imports*/
import flash.display.*;
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequestMethod; |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
public function Login():void
{
// We will use URLRequest to post our variables to a
// PHP file which will return variables back
// Set the location of your PHP file
var urlReq:URLRequest = new URLRequest ("login.php");
// Set the method to POST
urlReq.method = URLRequestMethod.POST;
// Define the variables to post
var urlVars:URLVariables = new URLVariables();
urlVars.userName = 'myUsername';
urlVars.userPass = 'myPassword';
// Add the variables to the URLRequest
urlReq.data = urlVars;
// Add the URLRequest data to a new Loader
var loader:URLLoader = new URLLoader (urlReq);
// Set a listener function to run when completed
loader.addEventListener(Event.COMPLETE, onLoginComplete);
// Set the loader format to variables and post to the PHP
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(urlReq);
}
public function onLoginComplete(event:Event): void
{
//retrieve success variable from our PHP
if(event.target.data.success == "true"){
trace("Login Complete");
}
} |
In our PHP file we can grab our variables and handle them in PHP. This makes it easy to create sessions and store in the database. Using GET parameters we can return variables back to our ActionScript file.
Save this as your login.php
|
1 2 3 4 5 6 7 8 9 |
<?php
//Grab username and password variables
$username = $_POST['userName'];
$password = $_POST['userPass'];
// for additional variables use the &
// success=true&username=$username&password=$password
echo "success=true";
?> |
» Download – Source Files for this tutorial
Included Files
- Main.fla – The main project file for the demo
- Main.as – The main ActionScript file for the demo
- login.php – PHP file to handle posted AS3 variables
- Main.html – HTML to view published swf
- Main.swf – Published swf movie file
- ReadMe.txt – Tutorial demo instructions
January 31, 2012 at 1:18 pm | Pat Jhopz
it is undefined. Help me.February 23, 2012 at 9:57 pm | admin
Can you please tell me more about the error your are getting? I have included demo files for this tutorial now, so hopefully that will clear any confusion.February 6, 2012 at 9:48 pm | Coder1
Does not work. Error: Scene 1, Layer 'Layer 1', Frame 1, Line 44 1114: The public attribute can only be used inside a package.February 23, 2012 at 9:47 pm | admin
The error you are getting is because you are trying to put the ActionScript into the first frame instead of in a package and class. If you are familiar with using a Document class in Flash with ActionScript 3, plugging in these functions and imports should work just fine. If you are not, I can write up a quick tutorial on how to get started with that. I have uploaded demo files for this tutorial here. It includes source files, additional comments and a published swf which should help. The files were saved in Flash CS4 so you will need that or a newer version.February 24, 2012 at 7:40 pm | ThatShiCray
It works! Thank you so much!