4 Replies Latest reply on Aug 31, 2009 12:10 PM by bocianek

    basic things - send short data to php file and retrieve it in flex

    bocianek Level 1

      I have file.php:

       

      <?php
      if(isset($_POST["variable1"]))
      {
           $x = $_POST["variable1"];
           echo $x;
      }
      ?>

       

       

       

      In Flex I have a textbox, a button and a label:


      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

       

          <mx:HTTPService id="UserRequest" url="file.php" useProxy="false" method="POST" result="ShowResult(event)">
          <mx:request xmlns="">
              <variable1>{t1.text}</variable1>
          </mx:request>
          </mx:HTTPService>   
        

          <mx:Script>
          <![CDATA[
             
              import flash.events.Event;
             
              private function ShowResult(event:Event):void
              {
                  l1.text = event;
              }
                 
          ]]>
          </mx:Script>

          <mx:TextInput x="243" y="67" id="t1"/>
          <mx:Button x="289" y="130" label="Button" id="b1" click="UserRequest.send()"/>
          <mx:Label x="301" y="188" text="Label" id="l1"/>
         
      </mx:Application>

       

       

       

      After write something in textbox and click button I would like to send text from textbox to file.php and retrieve text from file.php and show it in Flex label. How can I do that ? PHP file is good but I am new in Flex so something is wrong in Flex file, what ?

        • 1. Re: basic things - send short data to php file and retrieve it in flex
          matthew horn Level 3

          Couple of things jump out at me here:

           

          1) I would switch to GET requests. POST requests tend to be... peculiar... sometimes. So in your PHP, change it to $_GET and in your MXML, just remove the method="POST" from the HTTPService.

           

          2) Using proxies versus allowing access. This is a little trickier. I am not sure of your skill level, so I'll just give you my impressions of working with PHP. In general, I use proxies instead of using crossdomain.xml files to allow access. Remember that your development environment's sandbox will allow you to hit any PHP page anywhere to get data. Once deployed, though, you're either going to need a proxy page if you're ihitting a third-party domain or you're going to need a crossdomain.xml file on the target domain that allows access from your SWF.

           

          3) Make sure you are deploying the SWF file, the wrapper, and the PHP file all on the same server that is running PHP, and that you are using a network request (ie, "http://...") to access the application.

           

          4) If you're just running the SWF file as compiled from Flex Builder and requesting it as a file resource, then your relative request to the PHP file might not be pointed at the right location. I would adjust this to use a full URL so you are sure you are pointing to the right place.

           

          hth,

          matt horn

          flex docs

          • 2. Re: basic things - send short data to php file and retrieve it in flex
            bocianek Level 1

            English isn't my nation language so I understand 1% Your sentences. It must be POST - not GET - POST is safer and it can have more chars.

             

            I think something is wrong here:

             

            import flash.events.Event;

            private function ShowResult(event:Event):void
                    {
                        l1.text = event; //here I have error: Event to an unrelated type String.
                    }

            • 3. Re: basic things - send short data to php file and retrieve it in flex
              matthew horn Level 3

              Ah, ok. Try this:

               

              l1.text = event.result;

              1 person found this helpful
              • 4. Re: basic things - send short data to php file and retrieve it in flex
                bocianek Level 1

                thx - it works :


                <?xml version="1.0" encoding="utf-8"?>
                <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">


                    <mx:HTTPService id="UserRequest" url="file.php" useProxy="false" method="POST" result="ShowResult(event)" showBusyCursor="true">
                    <mx:request xmlns="">
                        <variable1>{t1.text}</variable1>
                    </mx:request>
                    </mx:HTTPService>
                   
                   
                    <mx:Script>
                    <![CDATA[
                       
                        import mx.rpc.events.ResultEvent;
                       
                        private function ShowResult(event:ResultEvent):void
                        {
                            l1.text = event.result.toString();
                        }
                           
                    ]]>
                    </mx:Script>



                    <mx:TextInput x="243" y="67" id="t1"/>
                    <mx:Button x="289" y="130" label="Button" id="b1" click="UserRequest.send()"/>
                    <mx:Label x="301" y="188" text="Label" id="l1"/>
                   
                </mx:Application>