Thursday, February 17, 2011

Send variables from AS3 to PHP using the POST method

Today we will learn how to send variables from AS3 to PHP using the POST method.

Here is the code for the AS3 file:

var myData:URLVariables = new URLVariables();
myData.firstName = "Kirill";
myData.lastName = "Poletaev";

var myRequest:URLRequest = new URLRequest("http://mysite.com/test.php");

myRequest.data = myData;
myRequest.method = URLRequestMethod.POST;

var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;

try
{
loader.load(myRequest);
}
catch (error:Error)
{
trace('Error: unable to load the document.');
}

We simply create an URLVariables object and add parameters to it. When the code is executed, flash sends a request to the file we've specified in the path. In this example, it is a php file.

Here's the code for the PHP:

<?php
$text=$_POST['firstName']." ".$_POST['lastName'];
$to="mymail@hotmail.com";
$subject="Message from php";
mail($to,$subject,$text,"Content-Type: text/plain; charset=utf-8");
?>

The code above sends a email to the specified adress with the information it received from AS3.

Using this method, you can create a mail form in AS3 and PHP and other applications.

Thanks for reading!

2 comments:

Albert Andrada said...

Really Nice Information,Thank You Very Much For Sharing.
Wordpress Development Company

Dony Prasetiyo said...

good article

gimana cara kirim file besar?

cara kirim file besar jarak jauh

Post a Comment