3 Replies Latest reply on Aug 21, 2011 5:59 PM by artguate

    Sending FormMail in Flex

    tkaranov

      I'm using Flash Builder 4.5 with ActionScript 3 and Flex 4.5.1 to build website that, among other things, will send form data as an email.


      I've been using the generic cgi-bin script "nms FormMail" for my form mail in HTML. Now I need to know how to send my flash data to this the script.

      All my search results for a solution refer to older Flash versions which use LoadVars sendAndLoad method.  I finally found out that I need to use a URLRequest instead.

      My latest attempt at sending my flash form data results in an error but it does appear that I'm getting to my server....

       

      Network Monitor Error:

      Error: GET request

      The HTML form fails to specify the POST method, so it would not  be correct for this script to
      take any action in response to  your request.
      If you are attempting to configure this form to run with FormMail,  you need to set the request
      method to POST in the opening form tag,  like this: 
      <form action="/cgi-bin/FormMail.pl" method="post";/>
      href="http://nms-cgi.sourceforge.net/">FormMail</a>           
      &copy; 2001-2003 London Perl Mongers 

       

      This is a sample of my HTML code:

       

      <form action="http://www.mysite.com/cgi-bin/FormMail.pl" method="post">

       

        <input type="hidden" name="recipient" value="info@mysite.com">
        <input type="hidden" name="subject" value="Contact Us Form">
        <input type="hidden" name="redirect" value="http://www.mysite.com/sucess.html">
        <input type="hidden" name="required" value="rename,email,comments">
        <input type="hidden" name="missing_fields_redirect" value="http://www.awningsofmarietta.com/missing.html">
        <input type="hidden" name="sort" value="order:rename,email,subject,comments,phone,checkbox">  

       

        <input name="rename" id="name" maxlength="20" >

        <input name="email" id="email" maxlength="50" >

        <textarea  name="comments" rows=6 cols=40 >

        <input name="phone" id="phone" maxlength="12" >

        <input type="checkbox" name="checkbox"  value="Sunesta" > Sunesta</br>
        <input type="checkbox" name="checkbox"  value="Sunstyle" > Sunstyle</br>

        <textarea  name="comments" rows=6 cols=40 > 

        <input type="submit" name="Send" value="Send" id="send">

       

      </form>

       

      This is my MXML file:

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                     xmlns:s="library://ns.adobe.com/flex/spark"
                     xmlns:mx="library://ns.adobe.com/flex/mx"
                     minWidth="955" minHeight="600" viewSourceURL="srcview/index.html">
         
          <s:creationComplete>
              <![CDATA[
                  mailService.send();
              ]]>
          </s:creationComplete>
         
          <fx:Declarations>
              <!-- Place non-visual elements (e.g., services, value objects) here -->
         
              <s:HTTPService id="mailService"
                             url="http://www.mysite.com/cgi-bin/FormMail.pl"/>
                         
          </fx:Declarations>
         
          <fx:Script>
             
               <![CDATA[
                  import flash.events.Event;
                  import flash.net.*;
                 
                  private function mailForm(event:Event):void  {           
                             
                  var myRequest:URLRequest = new URLRequest("http://www.mysite.com/cgi-bin/FormMail.pl");
                  var myLoader:URLLoader = new URLLoader();
                  var myVariables:URLVariables = new URLVariables();
                 
                  myVariables.action = "/cgi-bin/FormMail.pl";
                  myVariables.method = "post";
                  myVariables.recipient = "info@mysite.com";
                  myVariables.subject = "Contact Us Form";
                  myVariables.lastName = formLastName.text;
                  myVariables.nameType = formType.text;
                  myVariables.stoneColor = formStoneColor.text;
                 
                  myRequest.method = URLRequestMethod.POST;
                  myRequest.data = myVariables;
                 
                  function onLoaded(evt:Event):void {
                      trace("here we get the data back: "+myLoader.data);
                  }
                 
                  myLoader.addEventListener(Event.COMPLETE, onLoaded);
                  myLoader.load(myRequest);
                 
                  }   
              ]]>
          </fx:Script>
         
          <s:Form x="87" y="21" width="298" defaultButton="{submitBtn}">
              <s:FormHeading label="Spark Form Heading" />
              <s:FormItem label="Type:" width="280" >
                  <s:TextInput id="formType" />
              </s:FormItem>
              <s:FormItem label="Stone Color:" width="280">
                  <s:TextInput id="formStoneColor"/>
              </s:FormItem>
              <s:FormItem label="Last Name:" width="281">
                  <s:TextArea id="formLastName" height="25" />
              </s:FormItem>
              <s:FormItem width="241">
                  <s:Button id="submitBtn" label="Submit"
                            click="mailForm(event);"/>
              </s:FormItem>
          </s:Form>
      </s:Application>

       

      I know my MXML code is a mess but I've been trying so many things I'm lost trying to set the request method to POST.  I can't find a tutorial for 4.5 all the tutorial's I've found are for older versions.

       

      If this is the wrong forum, please advise which forum I should use to post my problem.

       

      Thanks for any help!

      Tracey