3 Replies Latest reply on Jul 13, 2006 1:21 AM by Antoni_H.

    Sending data to PHP Script

    Antoni_H.
      Hello folks,

      I got a problem passing data to a PHP Script which is an simple mailer script. I don't know where the problem is, reading the doc says to me this should not be too hard - but...

      So - here is the mxml code I generated, to declare the HTTP Service

      quote:


      <mx:HTTPService id="formSender" url=" http://www.myDomain.com/mailer.php" method="POST" showBusyCursor="true" useProxy="false">
      <mx:request>
      <firstname>
      {registrationModel.firstname}
      </firstname>
      <lastname>
      {registrationModel.lastname}
      </lastname>
      <email>
      {registrationModel.email}
      </email>
      <position>
      {registrationModel.position}
      </position>
      </mx:request>
      </mx:HTTPService>



      and here the function which calls the send command:

      quote:


      private function submitForm():void {
      formSender.cancel();
      formSender.send();
      formSender.showBusyCursor = true;
      Alert.show("Your request has been send successfully.", "Send Report", Alert.OK , this);
      resetForm();
      }




      Thats all. I don't know where the problem is. I am not recieving any mail, but calling the mailer.php in the browser works. So the php script works fine.

      please give me some help, cause it makes me nervous.
        • 1. Sending data to PHP Script
          ddanone
          Hi Antoni, you can delete the 'mx:request' section and pass the 'registrationModel' object into the send parameters.
          Code should be:

          <?xml version="1.0" encoding="iso-8859-1"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml">

          <mx:Script>
          <![CDATA[

          import mx.controls.Alert;

          public function submitForm():void {
          formSender.cancel();
          formSender.send(registrationModel);
          //formSender.showBusyCursor = true;
          Alert.show("Your request has been send successfully.", "Send Report", Alert.OK , this);
          //resetForm();
          }
          private function getResultOk(r:Number,event:Event):void{
          if(!r){
          Alert.show('Error sending data!!');
          return;
          }
          Alert.show( String(this.formSender.lastResult) );
          }
          ]]>
          </mx:Script>

          <mx:HTTPService id="formSender"
          url=" http://www.yourdomain.com/mailer.php"
          method="POST" showBusyCursor="true" useProxy="false"
          result="getResultOk(1,event)"
          fault="getResultOk(0,event)" >
          </mx:HTTPService>

          <mx:Model id="registrationModel">
          <firstname>{firstname.text}</firstname>
          <lastname>{lastname.text}</lastname>
          <email>{email.text}</email>
          <position>{position.text}</position>
          </mx:Model>

          <mx:Form width="100%" height="100%">
          <mx:TextInput id="firstname" text="pepe"/>
          <mx:TextInput id="lastname" text="lopez"/>
          <mx:TextInput id="email" text="pepelopez@email.com"/>
          <mx:TextInput id="position" text="director"/>
          <mx:Button label="send" click="submitForm()"/>
          </mx:Form>

          </mx:Application>

          ------------------------------------------
          And php...
          <?
          echo 'OK: Vars received:'.count($_POST)."\n";
          foreach($_POST as $k=>$v){
          echo $k.'='.$v."\n";
          }
          exit;
          ?>

          • 2. Re: Sending data to PHP Script
            peterent Level 2
            Is the PHP script receiving anything at all? If it is receiving empty values, then the trouble might be a timing issue. You have the following code:

            rivate function submitForm():void {
            formSender.cancel();
            formSender.send();
            formSender.showBusyCursor = true;
            Alert.show("Your request has been send successfully.", "Send Report", Alert.OK , this);
            resetForm();
            }

            In this function you invoke send() then you call resetForm() which I assume clears the fields. The problem is that the send() doesn't happen right then and there. The send actually happens after your function completes which would account for blank or missing data.

            A better approach is to add a result handler for the request and when that returns, post your Alert and clear the form.

            Since I can't see what is actually happening, this is just an educated guess on my part.
            • 3. Re: Sending data to PHP Script
              Antoni_H. Level 1
              @ peterent AND ddanone

              thanks a lot. I build in the suggested code by ddanone and it works fine now.

              thanks Peter, didn't know about the timing issue. Is it always that all commands are executed after a function runs complete?

              Another problem occured after I made the changes the fault event was called everytime a request was send. Now works everything fine. The problem was, that I run the application locally. After I uploaded it and run from the web: everything works how it should.

              Thanks