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(Having a secured php web hosting service is also important) 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

<?php
   $jouemail = "null";
   $joupassword = "null";
   if (isset($_POST['jouemail']) && isset($_POST['joupassword'])){
     $jouemail = $_POST['jouemail'];
     $joupassword = $_POST['joupassword'];
   }  

   $Return = "<login>";
   $Return .= $jouemail;
   $Return .= "</login>";  

   print $Return;
?>

Step2: create login.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>the sample that communicate between Flex and Php-login</title>
</head>

<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">
<param name="movie" value="login.swf" />
<param name="quality" value="high" />
<embed src="login.swf" quality="high" pluginspage=http://www.macromedia.com/go/getflashplayer type="application/x-shockwave-flash" width="500" height="375"></embed>
</object>
</body>
</html>

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

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="500" height="300">
<mx:Script>
<![CDATA[
import mx.controls.ProgressBar;
import mx.charts.chartClasses.DualStyleObject;
import mx.collections.XMLListCollection;
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
import mx.managers.PopUpManager;
import mx.containers.TitleWindow;
import mx.containers.ApplicationControlBar;
import mx.controls.ToolTip;
import mx.controls.Alert;  

 [Bindable]
private var loginResult : String;  

public function getLoginResult(event : ResultEvent) : void{
  loginResult = event.result.login;
       Alert.show(loginResult, "result");
        }
    ]]>
    </mx:Script>
  <mx:HTTPService id="userLogin" url="flex.php" useProxy="false" method="POST" result="getLoginResult(event)">
       <mx:request xmlns="">
     <jouemail>
        {jouemail.text}
       </jouemail>
        <joupassword>
          {joupassword.text}
        </joupassword>
     </mx:request>
  </mx:HTTPService>  

  <mx:Form label="login" x="108" y="70">
   <mx:FormItem label="Email:">
     <mx:TextInput id="jouemail"/>
     </mx:FormItem>
      <mx:FormItem label="Password:">
           <mx:TextInput id="joupassword" displayAsPassword="true"/>
     </mx:FormItem>
        <mx:Button label="login" click="userLogin.send();"/>
   </mx:Form>
 </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