Tuesday, March 29, 2011

Server side connection in flex using php

MXML File

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#4D4B4B, #626060]">
<mx:Style source="assets/css/style.css"/>
<mx:Script>
    <![CDATA[
        import mx.managers.PopUpManager;
        import mx.controls.Text;
        import mx.rpc.events.FaultEvent;
        import mx.rpc.events.ResultEvent;
        import mx.controls.Alert;

     [Bindable]
     public var newuserloginDetail:NewuserLogin;
     public var alertmsgbox:alertpopup;
     public var displayhos:HospitalL2details;
     public var saml:XML;
    
     public function loginpage(evt:ResultEvent):void
     
     {
   
    if(evt.result.loginsuccess == "yes")

    {
        errorlogin.text="";
        username.text="";
          password.text="";
        displayhos=new HospitalL2details();
        this.addChild(displayhos);
    
     }

    if(evt.result.loginsuccess == "no")

     {
      username.text="";
      password.text="";
      errorlogin.text="Invalid username / password";

      }
       
     }
     
          private function login():void
   
      {
          loginHTTPservice.send();
      }
         
   
   
   
     public function createnewuser(evt:ResultEvent):void
     
     {
        
         if(evt.result.newuser == "Error")

    {
        newuserloginDetail.errormsg2.text="Error";
    
     }

    if(evt.result.newuser == "Successfully Created")

     {
     newuserloginDetail.errormsg2.text="Successfully Created";
     alertmsgbox=new alertpopup();
     PopUpManager.addPopUp(alertmsgbox,this,true);
     PopUpManager.centerPopUp(alertmsgbox);
      }
     
     }
     
     public function removemsgpopup():void
     
     {
     PopUpManager.removePopUp(alertmsgbox);   
     }
     
      public function removeallpopup():void
     
     {
     PopUpManager.removePopUp(alertmsgbox);   
      PopUpManager.removePopUp(newuserloginDetail);   
     }
     
     
     public function newuser():void
   
      {
          if(newuserloginDetail.newpassword.text != newuserloginDetail.newpassword1.text || newuserloginDetail.fname.text=="" || newuserloginDetail.lname.text=="" || newuserloginDetail.newpassword.text=="" || newuserloginDetail.newpassword1.text=="")
          {
             
            }
           
            else {
         
          createnewuserrequest.send();
           
            }
      }
     
      private function cleartexfield():void
      {
          errorlogin.text="";
          username.text="";
          password.text="";
      }   
     
      private function fault(evnt:FaultEvent):void
     
      {
          displayerrorHTTP.text=evnt.fault.faultString+'\n'+evnt.fault.faultCode+'\n'+evnt.fault.faultDetail;
      }
     
      public function newuserlogindis():void
     
      {
          newuserloginDetail=new NewuserLogin();
          PopUpManager.addPopUp(newuserloginDetail,this,true);
          PopUpManager.centerPopUp(newuserloginDetail);
     }
     
      public function romovepopupnewuser():void
      {
          PopUpManager.removePopUp(newuserloginDetail);
      }
      

    ]]>
   
</mx:Script>

<mx:HTTPService url="http://localhost/passwordresult/passoutput.php" useProxy="false" fault="fault(event)" result="loginpage(event)" method="POST" id="loginHTTPservice">

<mx:request xmlns="">
    <usernameres>{username.text}</usernameres>
    <passwordres>{password.text}</passwordres>
</mx:request>

</mx:HTTPService>

<mx:HTTPService url="http://localhost/passwordresult/newusercreate.php" method="POST" useProxy="false" result="createnewuser(event)" fault="fault(event)" id="createnewuserrequest">
   
    <mx:request xmlns="">
    <newusername>{newuserloginDetail.lname.text}</newusername>
    <newuserpass>{newuserloginDetail.newpassword.text}</newuserpass>
    </mx:request>
   
</mx:HTTPService>

    <mx:Panel x="406" y="241" width="453" height="287" layout="absolute" title="Login Page">
        <mx:Label x="52" y="59" text="User Name:" fontFamily="Arial" fontSize="13" fontWeight="bold"/>
        <mx:Label x="61" y="119" text="Password:" fontSize="13" fontFamily="Arial" fontWeight="bold"/>
        <mx:TextInput x="166" y="57" id="username"/>
        <mx:TextInput x="166" y="117" id="password" displayAsPassword="true"/>
        <mx:Button x="166" y="188" label="Submit" click="login()"/>
        <mx:Button x="271" y="188" label="Clear" click="cleartexfield();"/>
        <mx:Label x="20" y="219" text="New User Login" fontWeight="bold" buttonMode="true" useHandCursor="true" color="#0C8FAB" textDecoration="underline" click="newuserlogindis();"/>
        <mx:Label x="166" y="153" id="errorlogin" width="257" color="#ED1807"/>
    </mx:Panel>
    <mx:Label x="406" y="536" id="displayerrorHTTP" width="225" height="58" color="#FFFFFF"/>
   
</mx:Application>

HospitalL2details.MXML Component

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%" backgroundColor="#4C4B4B">
<mx:Script>
    <![CDATA[
        import mx.core.Application;
        public function removethiscontainer():void
       
        {
            Application.application.removeChild(this);
        }
       
    ]]>
</mx:Script>

    <mx:Label x="24" y="14" text="Hospital Details" color="#F9FCFD" fontSize="16" fontFamily="Arial" fontWeight="bold"/>
    <mx:Label  right="10" y="19" buttonMode="true"  text="Logout" color="#FEFEFE" fontWeight="bold" fontFamily="Arial" fontSize="12" click="removethiscontainer();"/>
    <mx:Panel  left="10" right="10" top="64" y="57" width="100%" bottom="10" height="100%" layout="absolute" styleName="topspacepanel">
    </mx:Panel>
   
</mx:Canvas>

NewuserLogin.MXML Component

<?xml version="1.0" encoding="utf-8"?>
<mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" width="428" styleName="Panelcol" height="408" layout="absolute" borderColor="#6E6F6F">

<mx:Script>
    <![CDATA[
        import mx.core.Application;
       
       
        public function validationtext():void
       
        {
            if(fname.text=="" || lname.text=="" || newpassword.text=="" || newpassword1.text=="")
           
            {
              errormsg.text="Fill the All Fileds";   
            }
            else
            {
               
                errormsg.text="";   
            }
       
        }
       
        public function validationtext1():void
        {
           
           
            if(newpassword.text != newpassword1.text || newpassword.text=="")
           
            {
                errormsg1.text="Please Enter Same Password";
            }
            else
            {
                 
              errormsg1.text="";   
            }
        }
       
        public function cleartextfield():void
       
        {
            fname.text="";
            lname.text="";
            newpassword.text="";
            newpassword1.text="";
        }
       
       
    ]]>
</mx:Script>


    <mx:Label text="Name" x="30" y="53" fontSize="13" fontWeight="normal" fontFamily="Arial"/>
    <mx:Label text="User Name" x="30" y="99" fontSize="13" fontWeight="normal" fontFamily="Arial"/>
    <mx:Label text="Password" x="32" y="150" fontSize="13" fontWeight="normal" fontFamily="Arial"/>
    <mx:Label text="Conform Password" x="30" y="204" fontSize="13" fontWeight="normal" fontFamily="Arial"/>
    <mx:Button x="180" y="273" label="Submit" click="validationtext(),validationtext1(),Application.application.newuser(),cleartextfield();"/>
    <mx:Button x="281" y="273" label="Clear" click="cleartextfield();"/>
    <mx:TextInput x="180" y="54" id="fname"/>
    <mx:TextInput x="180" y="100" id="lname"/>
    <mx:TextInput x="180" y="151" displayAsPassword="true" id="newpassword"/>
    <mx:TextInput x="180" y="203" displayAsPassword="true" id="newpassword1"/>
    <mx:Label x="317" y="8" text="Close Window" fontWeight="bold" useHandCursor="true" buttonMode="true" color="#0C8FAB" textDecoration="underline" click="Application.application.romovepopupnewuser();"/>
    <mx:Label x="9" y="7" text="New User Login" fontWeight="bold" useHandCursor="true" color="#4D6F05" textDecoration="normal" fontSize="14" fontFamily="Arial"/>
    <mx:Label x="176" y="327" width="173" height="20" color="#F43705" id="errormsg"/>
    <mx:Label x="10" y="233" width="173" height="20" color="#F43705" id="errormsg2"/>
    <mx:Label x="177" y="177" width="173" height="20" color="#F43705" id="errormsg1"/>
    <mx:Image source="assets/images/newuser.jpg" width="100" height="96" x="30" y="251"/>
</mx:Panel>


alertpopup.MXML Component

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="272" height="118">
<mx:Script>
    <![CDATA[
            import mx.core.Application;
    ]]>
</mx:Script>
    <mx:Panel width="100%" height="100%" styleName="topspacepanel" layout="absolute">
        <mx:Label text="Do you want to create new user" x="8" y="39" fontWeight="bold"/>
        <mx:Label text="Successfully Created" x="58" y="11" fontWeight="bold" fontSize="12" color="#FC0D0D"/>
        <mx:Button x="55" y="68" label="Yes" click="Application.application.removemsgpopup();" />
        <mx:Button x="140" y="68" label="No" click="Application.application.removeallpopup();" />
       
    </mx:Panel>
</mx:Canvas>


style.CSS

/* CSS file */
.Panelcol {
   borderColor: #6e6f6f;
   borderAlpha: 1;
   borderThicknessLeft: 10;
   borderThicknessTop: 0;
   borderThicknessBottom: 11;
   borderThicknessRight: 10;
   roundedBottomCorners: true;
   cornerRadius: 9;
   headerHeight: 22;
   backgroundAlpha: 1;
   highlightAlphas: 0.5, 0;
   backgroundColor: #ffffff;
   dropShadowEnabled: true;
   shadowDistance: 2;
   shadowDirection: left;
}

.topspacepanel

{
   borderAlpha: 0.59;
   borderThicknessLeft: 5;
   borderThicknessTop: 0;
   borderThicknessBottom: 4;
   borderThicknessRight: 4;
   roundedBottomCorners: false;
   cornerRadius: 0;
   headerHeight: 5;
   backgroundAlpha: 1;
   highlightAlphas: 0.06, 0.19;
   dropShadowEnabled: true;
   shadowDistance: 2;
   shadowDirection: left;


}
Your file keep in this Format



Server Side Files
passoutput.php

<?php
$serv ='localhost';
$usern ='root';
$passw ='';
$databasename = 'flexsample';

$sqlcon = mysql_connect($serv, $usern, $passw) or die(mysql_error());
mysql_select_db($databasename);

$username=mysql_real_escape_string($_POST["usernameres"]);
$password=mysql_real_escape_string($_POST["passwordres"]);

$query = "SELECT * FROM hospitallogin WHERE username = '$username' AND pass = '$password'";
$result = mysql_fetch_array(mysql_query($query));

$output = "<loginsuccess>";

if(!$result)
{
$output .= "no";       
}
else{
$output .= "yes";   
}

$output .= "</loginsuccess>";

//output all the XML
print ($output);
mysql_close($sqlcon);

?>
newusercreate.PHP

<?php
$lhoste='localhost';
$uname='root';
$passval='';
$DatabaseN='flexsample';

$sqlcon = mysql_connect($lhoste, $uname, $passval) or die(msql_error());
mysql_select_db($DatabaseN);

$newusername = mysql_real_escape_string($_POST['newusername']);
$newpasdwod = mysql_real_escape_string($_POST['newuserpass']);

$query = "INSERT INTO hospitallogin VALUES('$newusername','$newpasdwod')";
$result = mysql_fetch_array(mysql_query($query));

$output = "<newuser>";

if(!$result)
{
$output .= "Successfully Created";
}
else{
$output .= "Error";
}

$output .= "</newuser>";
print ($output);
mysql_close($sqlcon);
?>

MYSQL  FORMAT




Output Screen Shots

                                     

                                                                                                                
                                      






Wednesday, March 23, 2011

Upload File From Flex to PHP

MXML File

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init();" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#4E4C4C, #4E4C4C]">
    <mx:Script>
        <![CDATA[
           
            import mx.controls.Alert;
            private var urlRequest:URLRequest;
            private var fileReferenceList:FileReferenceList;
            private var serverSideScript:String = "http://localhost/testfile/uploadFile.php";
           
       
            private function init():void {
           
                fileReferenceList = new FileReferenceList();
                fileReferenceList.addEventListener(Event.SELECT, fileSelectedHandler);
            }
           
            private function uploadFile():void {
                fileReferenceList.browse();
            }
           
            private function fileSelectedHandler(event:Event):void {
                urlRequest = new URLRequest(serverSideScript);
                var fileReference:FileReference;
                var fileReferenceList:FileReferenceList = FileReferenceList(event.target);
                var fileList:Array = fileReferenceList.fileList;

                // get the first file that the user chose
                fileReference = FileReference(fileList[0]);
               
                // upload the file to the server side script
                fileReference.addEventListener(Event.COMPLETE, uploadCompleteHandler);
                fileReference.upload(urlRequest);
               
                // update the status text
                statusText.text = "Uploading...";
            }
           
            private function uploadCompleteHandler(event:Event):void {
                statusText.text = "File Uploaded: " + event.target.name + event.target.size;
            }

           
        ]]>
    </mx:Script>
   
    <mx:Label text="Upload File From Flex to PHP" fontWeight="bold" color="#FEFFFF" fontSize="23"/>
    <mx:Label text="Choose a file..." id="statusText" color="#FCFDFD" fontSize="17"/>
    <mx:Button click="uploadFile();" label="Upload File"/>
    </mx:Application>


PHP Server Side File

<?php
$url = "D:/uploads/"; //This is Upload file URL
$tempFile = $_FILES['Filedata']['tmp_name'];   // This is going to return the temporary file location
$fileName = $_FILES['Filedata']['name'];  // This is going to return the name.
$fileSize = $_FILES['Filedata']['size'];  // This is going to return the file size in bytes
move_uploaded_file($tempFile, $url.$fileName); //This is going to upload file to your folder
?>

Output