Communication between PHP and Flash using POST method of HTTPService service:-
Here I have created Flex project which passes two variables to PHP and get the sum back from PHP and displays the same in Flex. You need to send the variables in “name” “value” to PHP page as shown in code, same name value pair can be passed as Sum_HttpServ.send({num1:’3′,num2:’6′});
Below is the code written in Flex application:-
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” creationComplete=”fnCreationComplete()”>
<mx:HTTPService id=”Sum_HttpServ” url=”http://yourDomain/someFolder/GetSum.php” result=”fnDiplayResult(event)” fault=”fnHandleFault(event)”/>
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
private function fnCreationComplete():void{
var Obj:Object=new Object();
Obj.num1=2;
Obj.num2=5;
Sum_HttpServ.method="POST";
Sum_HttpServ.send(Obj);
}
private function fnHandleFault(event:FaultEvent):void{
Alert.show("Error ID="+event.fault.errorID+" faultString="+event.fault.faultString);
}
private function fnDiplayResult(event:ResultEvent):void{
Alert.show(event.result.toString());
}
]]>
</mx:Script>
</mx:Application>
Below is the code written in the PHP “http://yourDomain/someFolder/GetSum.php” page which is placed on remote machine in virtual directory
<?
$Number1=$_POST['num1'];
$Number2=$_POST['num2'];
print ($Number1+$Number2);
?>
To use GET method, you need to change Sum_HttpServ.method=”POST”; in flex project and use $_GET[‘num1'] and $_GET[‘num2'] in PHP page.
Getting Multiple parameters from PHP:-
To return the multiple parameters from PHP you can return the variables separated by ampersand (&) if you are using flashvars as resultFormat. There are fore more formats namely “text”, “array”, “xml”, “e4x” you can use anyone as per your need but depending on that you will have to format your response object in PHP. Here I am using flashvars as result format.
Below is the PHP code that returns addition & multiplication of variable.
<?
$Number1=$_POST['num1'];
$Number2=$_POST['num2'];
$Multiplication=$Number1*$Number2;
$Addition=$Number1+$Number2;
print “Addition=$Addition&Multiplication=$Multiplication”;
?>
And now you are ready to send the variables from PHP. Now in flex application, your fnDiplayResult() function will change to
private function fnDiplayResult(event:ResultEvent):void{
Alert.show(“Addition = “+event.result.Addition);
Alert.show(“Multiplication = “+event.result.Multiplication);
}
And add line Sum_HttpServ.resultFormat=”flashvars”; in fnCreationComplete() function before you send the parameters to PHP.
July 19, 2007 at 11:39 am |
Thanks Nitin for sharing this! Really helpful
-Din’s
July 19, 2007 at 11:48 am |
Good one Nitin!
July 20, 2007 at 10:03 am |
[...] However, you can also use HTTPService to send and load data in Flex. Nitin has posted a simple example of using HTTPService in flex. Check it out here. [...]
August 23, 2007 at 6:16 pm |
Thank you, for showing me what noone else could…
September 27, 2007 at 10:55 am |
its good Nitin.can u tell me how data from database display in combobox in flex through php.
October 5, 2007 at 7:52 pm |
Hi Shital, first get the data from database in any format you like, then you need to convert it to Arraycollection of string objects. And then you can use this arraycollection as a data provider for the combo box.
October 9, 2007 at 10:01 am |
thanks Nitin .I have done it.
November 5, 2007 at 9:43 am |
Hello Nitin ,i need ur help again. can u tell me richeditor data saved in sql ,how display in grid and again in richtexteditor for edit?
November 21, 2007 at 4:30 am |
That’s a really useful tutorial ..thanks!
December 9, 2007 at 10:38 am |
Hello sir,
Im new to flex and php.
I hav avg knwldge in flex and php as well as mysql.
My question is,
I need a simple example and description to retrieve data stored in mysql table to get displayed inside flex application text component.
Ie., ex : retrieving password field from mysql data into text component inside flex at run time while we click any button or we check any checkbox.
plz help me.
Im trying this from a week.
plz….
January 29, 2008 at 7:12 am |
HI Nitin,
I have a query related to this blog.
I need the username and password to my flex project ( this project link will be called from another file as a hyper link ).
Just for an example we are calling a *.php file with getUrl method in as2.0 and by using post method we can send values to the target php file .
I need the opposite thing it means i want to get the values which are send to my link by post method in flex file.
Gunjan
March 10, 2008 at 2:46 pm |
hi Nitin, this is Rahul, i am also a flash programmer but have only 1.5 years of experience, i am working in flash roomplanner project there is one problem that is how can i save my xml. because all the objects are coming from xml and. its admin part in .net nuke, admin can change objects what ever like .png, .jpg, .swf anything, i can able to save in temperory but client want it should save in xml through .net, I am serching about this question only, i need your help, if you can give me any example or if you can help me, it would be great help from you to me
thanks
Rahul
May 9, 2008 at 8:00 am |
In flex2 ..I have added a button tag inside the Form tag. like this:
changing the x of button doesnt shows any effect..the button is shown at its original position.I want to change its x..wot to do??
June 19, 2008 at 2:57 am |
Somehow i missed the point. Probably lost in translation
Anyway … nice blog to visit.
cheers, Kidnap.
July 10, 2008 at 3:07 pm |
how can i send a variable from a php page to a flex page and use it in the actionscript of the flex page
August 18, 2008 at 2:59 pm |
hi nitin,
I am a flash programmer, having 3 yrs exp in web development and flash programming ….. i wanna learn adobe flex builder….plz suggest me some books….or any training institute in pune where i could be certified flex developer.
Ashwini parasnis
web developer
Nilson technology pune
February 10, 2009 at 9:55 am |
hi nitin thank u for your blog
February 22, 2009 at 10:25 am |
Really thanks yar, it’s simple and easy to approach