sample code which guide you on how to communicate between Flex and Php

This article is writing for a Flex newbie like me. :) , guides a Flex newbie on how to communicate between Flex and Php.There are different ways of connecting Web clients written in Flex with the server-side applications being that Java, PHP, Ruby on Rails, ASP or anything else that can generate HTTP responses.Using HTTPService MXML tag,it can communicate with your server application.The below is the sample code,built with Flex 2 SDK.

Step1: create flex.php

Download: flex.php
  1. <?php 
  2.    $jouemail = "null"
  3.    $joupassword = "null";
  4.    if (isset($_POST['jouemail']) && isset($_POST['joupassword'])){ 
  5.      $jouemail = $_POST['jouemail']
  6.      $joupassword = $_POST['joupassword'];
  7.    } 
  8.  
  9.    $Return = "<login>";
  10.    $Return .= $jouemail;
  11.    $Return .= "</login>"
  12.  
  13.    print $Return
  14. ?>

Step2: create login.php

Download: login.php
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3.  
  4. <head
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  6. <title>the sample that communicate between Flex and Php-login</title
  7. </head>
  8.  
  9. <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0 width="500" height="375" title="flex">
  10. <param name="movie" value="login.swf" />
  11. <param name="quality" value="high" />
  12. <embed src="login.swf" quality="high" pluginspage=http://www.macromedia.com/go/getflashplayer type="application/x-shockwave-flash" width="500" height="375"></embed>
  13. </object>
  14. </body>
  15. </html>

Step3: create login.swf/login.mxml,put 3 file at the same folder

Download: login.mxml
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="500" height="300"
  3. <mx:Script
  4. <![CDATA[ 
  5. import mx.controls.ProgressBar
  6. import mx.charts.chartClasses.DualStyleObject
  7. import mx.collections.XMLListCollection
  8. import mx.collections.ArrayCollection
  9. import mx.rpc.events.ResultEvent
  10. import mx.managers.PopUpManager
  11. import mx.containers.TitleWindow
  12. import mx.containers.ApplicationControlBar
  13. import mx.controls.ToolTip
  14. import mx.controls.Alert
  15.  
  16.  [Bindable] 
  17. private var loginResult : String
  18.  
  19. public function getLoginResult(event : ResultEvent) : void{ 
  20.   loginResult = event.result.login
  21.        Alert.show(loginResult, "result")
  22.         } 
  23.     ]]
  24.     </mx:Script> 
  25.   <mx:HTTPService id="userLogin" url="flex.php" useProxy="false" method="POST" result="getLoginResult(event)"> 
  26.        <mx:request xmlns=""> 
  27.      <jouemail> 
  28.         {jouemail.text} 
  29.        </jouemail
  30.         <joupassword
  31.           {joupassword.text} 
  32.         </joupassword> 
  33.      </mx:request
  34.   </mx:HTTPService> 
  35.  
  36.   <mx:Form label="login" x="108" y="70"> 
  37.    <mx:FormItem label="Email:"> 
  38.      <mx:TextInput id="jouemail"/
  39.      </mx:FormItem> 
  40.       <mx:FormItem label="Password:"> 
  41.            <mx:TextInput id="joupassword" displayAsPassword="true"/
  42.      </mx:FormItem> 
  43.         <mx:Button label="login" click="userLogin.send();"/
  44.    </mx:Form> 
  45. </mx:Application>
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

2 Responses to “sample code which guide you on how to communicate between Flex and Php”

  1. judy says:

    Thanks so very much for taking your time to create this very useful and informative site. I have learned a lot from your site. Thanks!!r

  2. ppmohapatra says:

    hi I found it to be a very useful sample code.
    but, since in login.mxml, components other than ResultEvent and Alert are not used I want to comment out other import statements.

    Can anybody tell me what is the line comment notation or block comment notation!

    There are couple of other doubts also.
    viz. what is the advantage of having login.swf object embedded inside login.php (we can directly use login.swf as url)!! what technology is used for the 3 boxes in this page where the sample code is mentioned!!!

Leave a Reply