0 Replies Latest reply on Jun 15, 2011 7:47 AM by flexx0r

    Flex/PHP problem lastResult does not work correctly

    flexx0r Level 1

      hi flex community,

       

      I am trying to develope a small login but I am still new to flex. It's based a little bit on the tutorial "Flex Test Drive: Build an application in an hour". I have a mysql database where the username and the password are stored in.

       

      My Flex file looks like this:

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
                                  xmlns:s="library://ns.adobe.com/flex/spark"
                                  xmlns:mx="library://ns.adobe.com/flex/mx"
                                  xmlns:loginservice="services.loginservice.*"
                                  currentState="Login"
                                  width.Login="719" height.Login="606"
                                  width.Main="975" height.Main="700" xmlns:valueObjects="valueObjects.*">
           <fx:Script>
                <![CDATA[
                     import mx.controls.Alert;
                     import mx.events.FlexEvent;
                     
                     protected function buttonLogin_clickHandler(event:MouseEvent):void
                     {
                          loginResult.token = loginService.login(username.text, password.text);
                          trace(vOLogin.username);
                          trace(vOLogin.password);
                          trace(vOLogin.valid)
                          
                          /*if (vOLogin.valid == true) {
                               
                               currentState = "Main";
                          }*/
                     }
                     
                ]]>
           </fx:Script>
           <s:states>
                <s:State name="Login"/>
                <s:State name="Main"/>
           </s:states>
           <fx:Declarations>
                <valueObjects:VOLogin id="vOLogin" />
                <s:CallResponder id="loginResult" result="vOLogin = loginResult.lastResult[0] as VOLogin"/>
                <loginservice:LoginService id="loginService" 
                                                 fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)"
                                                 showBusyCursor="true"/>
                <!-- Place non-visual elements (e.g., services, value objects) here -->
           </fx:Declarations>
           
           <s:TextInput id="username" includeIn="Login" x="118" y="37" width="107"/>
           <s:TextInput id="password" includeIn="Login" x="118" y="88" width="107" displayAsPassword="true"/>
           <s:Label includeIn="Login" x="20" y="38" width="84" height="22" text="Benutzername:"
                      textAlign="right" verticalAlign="middle"/>
           <s:Label includeIn="Login" x="20" y="88" width="84" height="22" text="Passwort:"
                      textAlign="right" verticalAlign="middle"/>
           <s:Button id="buttonLogin" includeIn="Login" x="22" y="143" width="195" label="Login"
                       click="buttonLogin_clickHandler(event)"/>
      
      </s:WindowedApplication>

       

      and my 2 php Files look like this:

      VOLogin.php

       

      <?php
      class VOLogin {
           public
      $id;
           public
      $valid;
           public
      $msg;
            public
      $username;
            public
      $password;
           public
      $versionnumber;
      }
      ?>

       


      And this is my LoginService.php file:

       

      <?php
      require_once('VOLogin.php');
      require_once(
      'config.php');

      //$foo = new LoginService();
      //var_dump($foo->login("heinz", "heinz"));

      class LoginService {
      /**
      * Retrieve all the records from the table
      * @return an array of VOBenutzer
      * @param string $username
      * @param string $password
      */
      public function login($username, $password) {
         
         
      $mysql = mysql_connect(DATABASE_SERVER, DATABASE_USERNAME, DATABASE_PASSWORD);
         
      mysql_select_db(DATABASE_NAME);
         
      $SQL = "select * from flex where username='".$username."' and password='".$password."'";
         
      $r = mysql_query($SQL);
          
          if(
      mysql_num_rows($r) == 1) {
             
      $row = mysql_fetch_object($r);
          
             
      $tmp = new StdClass();
             
      $tmp->id = $row->id;
             
      $tmp->valid = true;
             
      $tmp->msg = "Login Ok";
             
      $tmp->username = $row->username;
             
      $tmp->password = $row->password;
             
      $tmp->versionnumber = $row->versionnumber;
             
      $output[] = $tmp;
                          
          }else {
              
             
      $tmp = new StdClass();
             
      $tmp->id = 0;
             
      $tmp->valid = false;
             
      $tmp->msg = "Benutzername oder Passwort falsch";
             
      $tmp->username = $username;
             
      $tmp->password= $password;
             
      $tmp->versionnumber = 0;
             
      $output[] = $tmp;
              
          }            
          
          return
      $output;
          
          }
      }
      ?>

       

       

      My actual problem is that I have to press 2 times on the button to get the correct trace information. With the help of the search function I found this thread:

       

      http://forums.adobe.com/thread/259280

       

      I am not sure if I have the same problem. If yes where should I put the eventhandler? I would be very happy for every useful answer.

       

       

      Greetings

      flexx0r