PHP and FLEX communication

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&#8221; layout=”absolute” creationComplete=”fnCreationComplete()”>
                <mx:HTTPService id=”Sum_HttpServ” url=”http://yourDomain/someFolder/GetSum.php&#8221; 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&#8221; 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.

About these ads

18 Responses to PHP and FLEX communication

  1. Dinesh says:

    Thanks Nitin for sharing this! Really helpful

    -Din’s

  2. Tushar says:

    Good one Nitin!

  3. [...] 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. [...]

  4. Koads says:

    Thank you, for showing me what noone else could…

  5. shital says:

    its good Nitin.can u tell me how data from database display in combobox in flex through php.

  6. Nitin Gore says:

    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.

  7. shital says:

    thanks Nitin .I have done it.

  8. Shital says:

    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?

  9. David says:

    That’s a really useful tutorial ..thanks!

  10. pavan says:

    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….

  11. Gunjan kumar says:

    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

  12. Rahul Kathet says:

    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

  13. kirat says:

    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??

  14. Kidnap says:

    Somehow i missed the point. Probably lost in translation :) Anyway … nice blog to visit.

    cheers, Kidnap.

  15. Nitish Dhar says:

    how can i send a variable from a php page to a flex page and use it in the actionscript of the flex page

  16. ashwini says:

    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

  17. ArunKing says:

    hi nitin thank u for your blog

  18. kishore kumar says:

    Really thanks yar, it’s simple and easy to approach

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: