0 Replies Latest reply on Aug 6, 2011 6:22 PM by mank02

    Problem Initializing my Application with the applicationComplete Event

    mank02

      Hi everyone!

       

      I'm having a problem initializing my application with the applicationComplete event in the main <mx:Application /> tag  in Flash Builder.

       

      I'm trying to add a check box to my login application. I'm using PHP to set the cookie on a local machine. The application works fine, with one exception: having set the cookie and refreshed the browser, the username and password values which I stored in the cookie, do not automatically reload into their corresponding text boxes!

       

      here's my mxml application file (That's left me scratching my head!):

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                  xmlns:s="library://ns.adobe.com/flex/spark"
                  xmlns:mx="library://ns.adobe.com/flex/mx" applicationComplete="init()">
        
         <fx:Declarations>
            <!-- Place non-visual elements (e.g., services, value objects) here -->
            <s:HTTPService id="getCookie" result="getCookie_resultHandler(event)" method="POST" url="getCookie.php"/>
            <s:HTTPService method="POST" id="login_user" result="checkLogin(event)" showBusyCursor="true" url="services/doLogin.php" useProxy="false">
               <s:request xmlns="">
                  <username>
                     {usernameTextInput0.text}
                  </username>
                  <password>
                     {passwordTextInput0.text}
                  </password>
                  <logincookie>
                     {setCookie.selected}
                  </logincookie>
               </s:request>
            </s:HTTPService>
         </fx:Declarations>
         <s:Group id="ikoLoginBox" bottom="-20" top="0" left="0" right="0" >
            <s:Button x="150" y="159" label="Login" click="login_user.send();" />
            <s:TextInput x="27" y="39" id="usernameTextInput0" width="193"  />
            <s:TextInput  x="27" y="92" id="passwordTextInput0" width="193" displayAsPassword="true" />
            <mx:FormItem x="27" y="67" label="Password" />
            <mx:FormItem x="27" y="14" label="Username"/>
            <s:CheckBox x="27" y="128.5" id="setCookie" label="Remember me" selected="{cookieAction}" />

         </s:Group>
         <fx:Script>
            <![CDATA[
               import mx.controls.Alert;
               import mx.rpc.events.ResultEvent;
              
               private var userid:int;
               private var usertype:String;
              
               [Bindable]
               private var cookieAction:Boolean;

               private function checkLogin(event:ResultEvent):void
               {
                  userid = event.result.loginsuccess;
                  usertype = event.result.usertype;
                  if (userid != 0)
                  {
                     currentState = usertype;
                     mx.controls.Alert.show('Well done. You are logged in');
                  }
                  else
                  {
                     mx.controls.Alert.show('Invalid username or password 8');
                  }
               }
               private function init():void
               {
                  getCookie.send();
               }  
               protected function getCookie_resultHandler(event:ResultEvent):void
               {
                  if(event.result.storedCookie == true)
                  {
                     mx.controls.Alert.show('cookie Action is true');
                     cookieAction = true;
                     usernameTextInput0.text = event.result.creds.username;
                     passwordTextInput0.text = event.result.creds.password;
                  }
                  else
                  {
                     mx.controls.Alert.show('cookie Action is false');
                     cookieAction == false;
                  }
               }
              
            ]]>
         </fx:Script>
      </mx:Application>

       

      Here's my PHP login file (in a /services subfolder):

       

      <?php

        define( "DATABASE_SERVER", "localhost" );
        define( "DATABASE_USERNAME", "root" );
        define( "DATABASE_PASSWORD", "" );
        define( "DATABASE_NAME", "ikousers" );

      //connect to the database
        $mysql = mysql_connect(DATABASE_SERVER, DATABASE_USERNAME, DATABASE_PASSWORD) or die(mysql_error());

      //select the database
        mysql_select_db(DATABASE_NAME,$mysql );

      //These are the variables that Flex is passing to PHP
        $username = mysql_real_escape_string($_POST["username"]);
        $password = mysql_real_escape_string($_POST["password"]);
        $logincookie = mysql_real_escape_string($_POST["logincookie"]);

      //Check the credentials
        $query = "SELECT * FROM school WHERE username = '$username' AND password = '$password'";
        $result = mysql_fetch_array(mysql_query($query));

      //Output the returned query in XML: If returned false output 0 else output the users id
        $output = "<loginsuccess>";
        if(!$result)
        {
           $output .= "0";
        }else{
           $output .= $result['id'];
        }
      $output .= "</loginsuccess>";

      //Output the User Type
        $usrtyp = "<usertype>";
        $usrtyp .= $result['userType'];
        $usrtyp .= "</usertype>";

      //If the checkbox is checked (true) set cookie, else (false) delete cookie
        if($logincookie == "true")
        {
        setcookie("username", $username, time()+3600);
        setcookie("password", $password, time()+3600);
        }else{
        setcookie("username", "", time()-1);
        setcookie("password", "", time()-1);
        }

      //output all the XML

        echo ($output);
        echo ($usrtyp);
        print ($lcook);
       
      ?>

       

      Here's my PHP get Cookie file (in a /services subfolder):

       


      <?php

      if($_COOKIE["username"])
        {
                print "<storedCookie>true</storedCookie>";

           print "<creds>
           <username>".$_COOKIE["username"]."</username>
           <password>".$_COOKIE["password"]."</password>
              </creds>";
        }
        else
        {
           print "<storedCookie>false</storedCookie>";
        }

      ?>
       

       

      Someone help me here, having successfully set the cookie, how can I now successfully implement the functionality to retrieve it? 

      Please tell me what I might've done wrong here.

      I'm working with:

       

      Flash Builder 4
      Flash Player 10.3
      mySQL 5.5.8
      And PHP 5.3.5