Post data from Actionscript 3 to PHP
If you need to send some data to a PHP script and don’t want to use services like Zend_AMF you can try posting that data directly to the PHP script.
With the code bellow I’ll actually send an email.
// ActionScript 3 Code
// PostToPHP.as
[actionscript]
package
{
import flash.events.Event;
import flash.events.HTTPStatusEvent;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
public class PostToPHP
{
// url to post
private var postToURL:String = “http://www.yourdomain.tld/post_receiver.php”;
public function PostToPHP()
{
// create request
var request:URLRequest = new URLRequest( postToURL );
// set post variables
var requestVars:URLVariables = new URLVariables();
requestVars.emailTo = ‘email.address@mailservice.tld’;
requestVars.subject = ‘This is a test email’;
requestVars.message = ‘An this is the content of the message.’;
// assign variables to the request and set request method type
request.data = requestVars;
request.method = URLRequestMethod.POST;
// load the request and listen for a response from the PHP script
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = “text”;
urlLoader.addEventListener(Event.COMPLETE, urlLoader_handler, false, 0, true);
urlLoader.load(request);
}
private function urlLoader_handler(e:Object):void
{
// the response given from the PHP script will be traced here
trace(e.target.data);
}
[/actionscript]
// PHP code
// post_receiver.php
// assign POST variables and clean them
// filter_var() is available from PHP 5.2.0
$emailTo = filter_var($_POST['emailTo'], FILTER_SANITIZE_EMAIL);
$subject = filter_var($_POST['subject'], FILTER_SANITIZE_STRING);
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
// send mail and remember function response
// boolean response
$mailSent = mail($emailTo, $subject, $message);
// the echoed response will be traced in actionscript
if($mailSent == true)
{
echo 'mail sent';
}
else
{
echo 'mail NOT sent';
}
Simple.
If you liked this post
you can buy me a beer


Insightful & clear Tutorial!
Well Done
Many Thanks