5 Replies Latest reply on Feb 19, 2010 6:19 AM by lopezio

    Flex with php

    lopezio

      Hi

      I want to insert a record in my database but ican't. I'm new in Flex and there is my code:

      <?xml version="1.0"?>

       

       

      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" layout="absolute" creationComplete="userRequest.send()">

       

       

      <mx:HTTPService id="userRequest" url="http://localhost/flex/insere.php" useProxy="false" method="POST">

       

       

      <mx:request xmlns="">

       

       

      <username>{username.text}</username><emailaddress>{emailaddress.text}</emailaddress>

       

       

      </mx:request>

       

       

      </mx:HTTPService>

       

       

      <mx:Form x="22" y="10" width="493">

       

       

      <mx:HBox>

       

       

      <mx:Label text="Username"/>

       

       

      <mx:TextInput id="username"/>

       

       

      </mx:HBox>

       

      <mx:HBox>

       

      <mx:Label text="Email Address"/>

       

      <mx:TextInput id="emailaddress"/>

       

      </mx:HBox>

       

      <mx:Button label="bolas" click="userRequest.send()"/>

       

      </mx:Form>

       

      </mx:Application>

      I think the problem is that insere.php isn't called.

      Any help, please?

        • 1. Re: Flex with php
          Matt Le Fevre Level 4

          try this:

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" layout="absolute">
          
               <mx:Script>
                   <![CDATA[
                   
                   [Bindable]
                   public var usernameStr:String;
                   [Bindable]
                   public var emailStr:String
                       
                       public function doThis():void
                       {
                           usernameStr = username.text;
                           emailStr = emailaddress.text;
                           
                           userRequest.send();
                       }
                   ]]>
               </mx:Script>
           
              <mx:HTTPService id="userRequest" url="http://localhost/flex/insere.php" useProxy="false" method="POST">
                  <mx:request xmlns="">
                      <username>{usernameStr}</username>
                      <emailaddress>{emailStr}</emailaddress>
                  </mx:request>
              </mx:HTTPService>
          
              <mx:Form x="22" y="10" width="493">
                  <mx:HBox>
                      <mx:Label text="Username"/>
                          <mx:TextInput id="username"/>
                      </mx:HBox>
                  <mx:HBox>
                      <mx:Label text="Email Address"/>
                      <mx:TextInput id="emailaddress"/>
                  </mx:HBox>
                  <mx:Button label="bolas" click="doThis()"/>
              </mx:Form>
          
          </mx:Application>
          

           

           

          if that doesn't work, post your php code

          • 2. Re: Flex with php
            lopezio Level 1

            Hi

            Thank you

            The problem it's: when i click the button nothing happens it seems that the inser.php is not called.

            Please look at my insere.php

            <?php
               Define( “DATABASE_SERVER”, “localhost” );
               Define( “DATABASE_USERNAME, “xxx” );
               Define( “DATABASE_PASSWORD”, “xxxx” );
               Define( “DATABASE_NAME”, “mariolxxopes” );
             
               //connect to the database
               $mysql = mysql_connect(DATABASE_SERVER, DATABASER_USERNAME, DATABASE_PASSWORD);
               mysql_select_db( DATABASE_NAME );
               if( $_POST["emailaddress"] AND $_POST["username"])
               {
                 //add the user
                 $Query = "INSERT INTO users VALUES ('', '".$_POST['username']."', '".$_POST['emailaddress']."')";
                 $Result = mysql_query( $Query );
               }

               //return a list of all the users
               $Query = "SELECT * from users";
               $Result = mysql_query( $Query );
               $Return = "<users>";

               while ( $User = mysql_fetch_object( $Result ) )
               {
                 $Return .= "<user><userid>".$User->userid."</userid><username>".$User->username."</username><emailad dress>".$User->emailaddress."</emailaddress></user>";
               }
               $Return .= "</users>";
               mysql_free_result( $Result );
               print ($Return)
            ?>

            • 3. Re: Flex with php
              archemedia Level 4

              You should set the result and fault properties in your HTTPService to monitor what's happening.

               

              In Flex:

               

               <mx:HTTPService id="userRequest" 
                   result="handleResult(event)" fault="handleFault(event)"
                   url="http://localhost/flex/insere.php"
                   useProxy="false" method="POST">
                      <mx:request xmlns="">
                          <username>{usernameStr}</username>
                          <emailaddress>{emailStr}</emailaddress>
                      </mx:request>
                  </mx:HTTPService>

               

              private function handleResult(evt:ResultEvent):void

              {

              }

               

              private function handleFault(evt:FaultEvent):void

              {

              }

               

              In your php script, add somewhere;

               

              echo "Database record entered succesfully";

               

               

              Dany

              1 person found this helpful
              • 4. Re: Flex with php
                Matt Le Fevre Level 4

                Heres the working flex:

                 

                <?xml version="1.0" encoding="utf-8"?>
                <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" layout="absolute" creationComplete="userRequest.send()">
                
                     <mx:Script>
                         <![CDATA[
                             import mx.rpc.events.ResultEvent;
                         
                         [Bindable]
                         public var usernameStr:String;
                         [Bindable]
                         public var emailStr:String
                             
                             public function doThis():void
                             {
                                 usernameStr = username.text;
                                 emailStr = emailaddress.text;
                                 
                                 userRequest.send();
                             }
                             
                             public function handleUsers(evt:ResultEvent):void
                             {
                                 try
                                 {
                                     userDG.dataProvider = evt.result.users.user;
                                 }
                                 catch(err:Error)
                                 {
                                     trace("no entries in the database found");
                                 }
                             }
                         ]]>
                     </mx:Script>
                 
                    <mx:HTTPService id="userRequest" result="handleUsers(event)" url="http://localhost/test.php" useProxy="false" method="POST">
                        <mx:request xmlns="">
                            <username>{usernameStr}</username>
                            <emailaddress>{emailStr}</emailaddress>
                        </mx:request>
                    </mx:HTTPService>
                
                    <mx:Form x="22" y="10" width="493">
                        <mx:HBox>
                            <mx:Label text="Username"/>
                                <mx:TextInput id="username"/>
                            </mx:HBox>
                        <mx:HBox>
                            <mx:Label text="Email Address"/>
                            <mx:TextInput id="emailaddress"/>
                        </mx:HBox>
                        <mx:Button label="bolas" click="doThis()"/>
                    </mx:Form>
                    
                    <mx:DataGrid id="userDG" x="22" y="138">
                        <mx:columns>
                            <mx:DataGridColumn headerText="User ID" dataField="userid"/>
                            <mx:DataGridColumn headerText="Name" dataField="username"/>
                            <mx:DataGridColumn headerText="Email" dataField="emailaddress"/>
                        </mx:columns>
                    </mx:DataGrid>
                
                </mx:Application>
                

                 

                Setup a datagrid so you can see the Button adding users as you press it

                 

                 

                 

                 

                Try this PHP, swap "xxx" for your database usename and "xxxx" for your database password:

                 

                <?php
                   $server = "localhost";
                   $user = "root";
                   $password = "test";
                   $dbname = "mariolxxopes";
                 
                   //connect to the database
                   $mysql = mysql_connect($server, $user, $password);
                   mysql_select_db($dbname);
                   
                   $userStr = mysql_real_escape_string($_POST["username"]);
                   $emailStr = mysql_real_escape_string($_POST["emailaddress"]);
                   
                   if(($userStr != "") && ($emailStr != ""))
                   {
                          //add the user
                          $Query = "INSERT INTO users VALUES ('', '$userStr', '$emailStr')";
                          $Result = mysql_query( $Query );
                   }
                
                   //return a list of all the users
                   $Query = "SELECT * from users";
                   $Result = mysql_query($Query);
                   $Return = "<users>";
                
                   while ($User = mysql_fetch_object($Result))
                   {
                          $Return .= "<user><userid>".$User->userid."</userid><username>".$User->username."</username><emailaddress>".$User->emailaddress."</emailaddress></user>";
                   }
                   $Return .= "</users>";
                   mysql_free_result($Result);
                   print ($Return)
                ?>
                

                 

                 

                 

                ta daa

                • 5. Re: Flex with php
                  lopezio Level 1

                  Thank you guys. It works fine and now I'll study the code.

                  This forum rocks.