Flash CS3 and PHP - Send and Receive data








Codes

sendtophp.as
package {
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.text.*;
public class sendtophp extends Sprite {
var phpFile:String = "sendDataToServer.php";
public function sendtophp():void {
callBtn.addEventListener(MouseEvent.CLICK, callServer);
}
function callServer(e:MouseEvent):void {
var urlRequest:URLRequest = new URLRequest(phpFile);

var urlParams:URLVariables = new URLVariables();
urlParams.sampleVal = sendTxt.text;

urlRequest.method = URLRequestMethod.POST;
urlRequest.data = urlParams;

var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, serverResponse);
loader.load(urlRequest);
}
function serverResponse(e:Event):void {
var loader:URLLoader = URLLoader(e.target);
var variables:URLVariables = new URLVariables(loader.data);
responseTxt.text = variables.returnValue;
}
}//end of class sendtophp

}//end of package

sendDataToServer.php
< ? php

print "returnValue= Hi ".$_POST['sampleVal'].", it's working";

?>


Source File - Sending2Server.zip


Comments

Trix said…
Hi, Thanks, you replied me for my question on yahoo.

However, you dint see what I mentioned "utf-8" characters - To be specific, some asian language fonts.

But I solved the problem by using ARIAL UNICODE MS font. (you could see what I have done in here

Thanks once again for taking time to reply me.
jessekenney said…
Could I have the php file request a query string. somehow. not sure how. but..
Anil said…
Hi jessekenney

Try this code


Flash
-----
navigateToURL( new URLRequest("sendDataToServer.php?sampleVal="+inputText.text),"_self");


here inputText.text is the text you have to send.

php
===

print "data from flash =".$_GET['sampleVal'];
Admin said…
Thank you so much for this awesome script. Who needs to use "flashvars" when you can retrieve variables through php

Popular posts from this blog

AS3 - Access objects from external SWF files

Flash CS3 - XML Guestbook (AS3)

Flash CS4 - 3D Rotating Menu