Using actionscript post flash form data to API via http post command

I have a flash newsletter subscriber sign up form with the following fields:
first, last, email and zip.

 I can interface my email service’s API through HTTP using a post command.
The http command is as follows with the variables from the flash form being appended after the ^
http://api.emailservice.com/bin/api_web?c=sub&r=myfoundation&data=email,first,last,zip,source^jane@domain.com,Jane,Doe,94965,dscc

The source field data I want to pass to the API is coming via a source code appended to the url

Using actionscript 2.0 please write the code that will:
1.       pass the url sourcecode  to my flash form
2.      Provide an example of the syntax I should use for the url using this as the base         http://www.myfoundation.com/signup.html to track the user’s source.
3.      Finish the action script that will create and send the http post command to the api

Thank You
pgobosAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

IqAndreasCommented:
Is there any reason you need this code to be in AS2?

Even though AS3 can be difficult to get a grip on at first, the improvements in speed, performance, and capabilities blows AS2 out of the water. I would be more than happy to provide you with help for an ActionScript 3 version of what you need. :)
0
pgobosAuthor Commented:
Sure provide the solution in AS3  Thanks
0
IqAndreasCommented:
Excellent.

SERVER SIDE
First of all, could you give me a description of how the API is set up that you are sending the emails with? Do you have a system in place already, or do you need that as well? Was the sample URL the actual text you need for the API, or just an example of how it could be done?

If you do not yet have an API set up, the usual "server side" system for this is PHP, as it is cheaper than ASP, and usually has an easy time interesting with a MySQL database. Also, if you use the POST protocol instead of GET (basically, GET just sends the information in the URL, POST keeps it hidden), you are allowed to send a lot more data. I can provide you with a sample PHP script if you should need it.


CLIENT SIDE (ActionScript)
See the attached code for the function used to send data to the server. I set it up so it can be pasted directly onto the timeline, but I can just as easily make it into a class. Use the function like this:
sendMail("John Doe", "john.doe@hotmail.com", "Hi! This is John Doe. I like your site, and I want to subscribe to your newsletter!", "<other information here>")

Note that the names of the URLVariables might need to be changed depending on how your server side API wants them named. I can easily do this for you if you provide me with more information.


If you want me to explain any of the code, just ask, and I will be more than happy to help.
function sendMail(senderName:String, senderEmail:String, messageText:String, otherDetails:String):void
{

     var request:URLRequest = new URLRequest("http://www.mysite.com/subscribe.php");
     
     var variables:URLVariables = new URLVariables();
     variables.sender_name = senderName;
     variables.sender_email = senderEmail;
     variables.message_text = messageText;
     variables.other_details = otherDetails;
     
     request.method = URLRequestMethod.POST;
     request.data = variables;
     
     var loader:URLLoader = new URLLoader();
     loader.addEventListener(Event.COMPLETE, onComplete);
     
     loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
     loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
     

     try 
     {
        loader.load(request);
     }
     catch (error:Error) 
     {
        trace("Unable to send to server. There was an error.");
     }
}

function onComplete(ev:Event)
{
    trace("The message was sent successfully!");
}

function onError(ev:Event)
{
    trace("There was a problem sending the message.");
}

Open in new window

0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

IqAndreasCommented:
I modified the code slightly so that it would match the sample API url you gave.

Notice how I combined GET and POST data. This is possible if you hardcode the GET data directly onto the URL.

If you have any further questions or details regarding the API you are using, I would be more than happy to help.

Andreas
function sendMail(email:String, firstname:String, lastname:String, zip:*, source:String):void
{

     var request:URLRequest = new URLRequest("http://api.emailservice.com/bin/api_web?c=sub&r=myfoundation");
     
     //data=email,first,last,zip,source^jane@domain.com,Jane,Doe,94965,dscc
     var variables:URLVariables = new URLVariables();
     variables.email = email;
     variables.first = firstname;
     variables.last = lastname;
     variables.zip = String(zip); //It may be easier if the zip is sent as a string instead of as a number
     variables.source = source;
     
     request.method = URLRequestMethod.POST;
     request.data = variables;
     
     var loader:URLLoader = new URLLoader();
     loader.addEventListener(Event.COMPLETE, onComplete);
     
     loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
     loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
     

     try 
     {
        loader.load(request);
     }
     catch (error:Error) 
     {
        trace("Unable to send to server. There was an error.");
     }
}

function onComplete(ev:Event)
{
    trace("The message was sent successfully!");
}

function onError(ev:Event)
{
    trace("There was a problem sending the message.");
}

Open in new window

0
pgobosAuthor Commented:
I am not sending emails the process I need help is collecting the flash form data from a sign up form and sending the requested information to my email services API.  The data passed will be used to subscribe a user and to post email,first,last,zip and source data to a user’s record.

I have several sign up forms and I want to pass a source code from the url to the flash form and then to the HTTP POST, so we can track where users are signing up from.

Example of URL and code being passed to sign up form
http://www.myfoundation.com/signup.html?source=acc

The flashform form collects the following information: email,first, last, zip
Example Data from flash form:
email =jane@domain.com
first= Jane
last= Doe
zip= 94965

Source code being passed from url:
source= acc

I can interface my email service’s API through HTTP using a post command.  
The http command is as follows with the variables from the flash form need to be appended after the ^

http://api.emailservice.com/bin/api_web?c=sub&r=myfoundation&data=email,first,last,zip,source^jane@domain.com,Jane,Doe,94965,acc

The first part of the http command up to the ^ is the syntax I need to use to access my services API.  As you can see from my example before the ^I am listing field names, and user information to the api.

My server side API wants the http post just like the above sample with the variables being appended after the ^ character.

Don’t know how flash will respond to this or how it can be used but when the record is added the API will return the following message:

SUCCESS: Total Records Processed 1, Total Subscriptions 1, Records Added 1, Records Updated 0, Records Ignored (Optout Error) 0, Records Failed Other Error 0
0
pgobosAuthor Commented:
Thanks Andreas  I posted my comment before noticing your response.  I hope my comment clarifies what I am trying to accomplish.  Also as you can see from my comment the "source" variable is not coming from the flash form, but the url.  I want to pass the source code from the url to the flash form and use it in the http post command.
0
IqAndreasCommented:
Okay, I see now. Thank you for clarifying.

Give the new attached source a try.

I am going to have to get back to you on the "source" part, as that part may require either some PHP or JavaScript code in order to send the variable to Flash, and is separate from the method of sending data to your server.

However, the rest of the code will work just fine for now, and you can just insert a dummy value for the "source" parameter until I get back to you (shouldn't be more than 20 minutes or so)
function sendToServer(email:String, firstname:String, lastname:String, zip:*, source:String):void
{

     var url:String = "http://api.emailservice.com/bin/api_web?c=sub&r=myfoundation&data=email,first,last,zip,source^";

     //Add the variables to the URL
     url += email + "," + firstname + "," + lastname + "," + String(zip) + "," + source;

     var request:URLRequest = new URLRequest(url);
     
     
     var loader:URLLoader = new URLLoader();
     loader.addEventListener(Event.COMPLETE, onComplete);
     
     loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
     loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
     

     try 
     {
        loader.load(request);
     }
     catch (error:Error) 
     {
        trace("Unable to send to server. There was an error.");
     }
}


function onComplete(ev:Event)
{
    //Print out the return message from the server
    var message:String = Loader(ev.currentTarget).data;
    trace(message);
}

function onError(ev:Event)
{
    trace("There was a problem sending the message.");
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
IqAndreasCommented:
Okay, I found a few links which should lead you down the right track on retrieving the "source" variable from the URL.

The first method uses ExternalInterface to call JavaScript:
http://blog.circlecube.com/2008/03/tutorial/get-current-url-and-query-string-parameters-to-flash-tutorial/

And the second method uses FlashVars to send parameters to the SWF:
http://blogs.adobe.com/pdehaan/2006/07/using_flashvars_with_actionscr.html

Which of the two systems you implements mostly depends on your hosting and the way the SWF is embedded on the site.


Just ask if the guides are unclear or if you want me to help you implement the information with your current code.

Best regards,
Andreas
0
IqAndreasCommented:
One more thing, you might be able to try is using the "loaderInfo" to access the variables. Give this code a try and see if it works:
trace(root.loaderInfo.parameters.source);

Sorry, I rarely deal with Flash interaction with the website. Most of my current projects are all desktop applications in Flex or AIR, and are never really published to the site, so I rarely have to deal with embedding issues.
0
pgobosAuthor Commented:
Thanks Andreas  for all your help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Adobe Flash

From novice to tech pro — start learning today.