1 Reply Latest reply on Sep 23, 2006 5:27 PM by chris.huston.t10

    Connection and retreiving from a phpscript..

    HeathC
      G'day Everyone,

      Im fairly new at this and I'll admit Im a bit confused by some of the code I've seen (that and the hello world example threw me for a loop once we added in the extra code...) but theres a couple things Im very good at, database design and php scripting (except makng it look good, which is why I've come to flex...)

      While the documentation is very good, I've hit a few snags...and im sure you guessing where I leading to...you guessed it URLRequest/URLVariables/URLLoader...

      Heres my complete mxml code:

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

      <mx:Script>
      <![CDATA[
      //public class ServiceDelegate {

      public function getRemoteXML(listener:Function, username:String, password:String):String
      {

      var urlloader:URLRequest = new URLRequest(" http://localhost/flex_tests/dat.php");
      urlloader.method = "POST";
      var vars:URLVariables = new URLVariables();
      //vars.serviceurl = url;
      vars.username = username;
      vars.password = password;
      urlloader.data = vars;

      var xmlloader:URLLoader = new URLLoader(urlloader);
      xmlloader.addEventListener(Event.COMPLETE, listener);
      return xmlloader.data;
      }
      //}

      public function mylisten(event:Event):void
      {
      var dataXML:XML = XML(event.target.data);

      }

      public function loadit(pname:String,ppass:String):void
      {
      //var myClass = new ServiceDelegate();

      mainTxt.text = getRemoteXML(mylisten,pname,ppass);
      }
      ]]>
      </mx:Script>

      <mx:TextArea id="mainTxt" width="400" backgroundColor="#DDDDDD" editable="false" />
      <mx:HBox width="400">
      <mx:Label text="User Name:"/>
      <mx:TextInput id="userNameTxt" width="100%" />
      </mx:HBox>
      <mx:HBox width="400">
      <mx:Label text="User Name:"/>
      <mx:TextInput id="passwordTxt" width="100%" />
      </mx:HBox>
      <mx:Button label="Click" click="loadit(userNameTxt.text,passwordTxt.text);"/>
      </mx:Application>




      And heres my php code (yes its simple but its just to test to see if it works...)

      <?php
      header('Content-Type: text/xml');
      $xmlstring = '<?xml version="1.0" encoding="utf-8"?>';
      $xmlstring .= '<response>';
      $xmlstring .= '<uname>'.$_POST["username"].'</uname>';
      $xmlstring .= '<pwrd>'.$_POST["password"].'</pwrd>';
      $xmlstring .= '</response>';

      print $xmlstring;
      ?>

      Now when its built I try clicking the button after entering data and I get no response......any ideas?
        • 1. Re: Connection and retreiving from a phpscript..
          chris.huston.t10 Level 3
          HeathC

          Your code works for me. I get the XML returned from the PHP script with the username and password. Try adding a trace to your myListen function and see if you get anything.

          trace(XML(event.target.data));

          So it seems that your function is receiving the XML. Do you want to display the received XML somewhere? You can assign the XML data to your textArea with by adding the below code to your myListen function:

          mainTxt.text = "Your username is: " + dataXML.uname + "\r" + "Your password is: " + dataXML.pwrd;

          Vygo