Ok - this is making use of an AS3 class to wrap all the functionality. So you need to take the following and save it as "CMRequest.as" in the same folder as your fla file:
PHP Code:
package {
import flash.events.*;
import flash.net.*;
public class CMRequest extends EventDispatcher {
public static const ON_FINISHED = "OnFinished";
private static const INVALID_EMAIL = 1; // error code for an invalid email
private static const SUCCESS = 0; // code for successful request
private static const INVALID_API_KEY = 100; // error code for a invalid api key
private static const INVALID_LIST_ID = 101; // error code for a invalid list id
private var urlLoader:URLLoader; // the loader that gets the info from campaign monitor
private var _response:String; // the response message to show the user
/*
* @param apiID - the campaign monitor api id number
* @param listID - the campaign monitor list id number
* @param firstName - the first name text field of the participant
* @param lastName - the last name text field of the participant
* @param optionalFields - the rest of the fields that you need for campaign monitor.
* NOTE: These must have the same name variable name as optional fields on campaign monitor.
*
*/
public function CMRequest(apiID, listID, firstName, lastName, email, ... optionalFields) {
var myApiId = apiID;
var myListId = listID;
// build the soap call with appropriate content
var theSoapCall = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<SOAP-ENV:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
"<SOAP-ENV:Body><Subscriber.AddWithCustomFields xmlns=\"http://api.createsend.com/api/\">";
theSoapCall += "<ApiKey>" + myApiId + "</ApiKey>";
theSoapCall += "<ListID>" + myListId + "</ListID>";
theSoapCall += "<Email>" + email.text + "</Email>";
theSoapCall += "<Name>" + firstName.text + " " + lastName.text + "</Name>";
// add all the custom fields to the soap call
theSoapCall += "<CustomFields>";
for (var i=0; i < optionalFields.length; i++) {
theSoapCall += "<SubscriberCustomField><Key>[" + optionalFields[i].name + "]</Key><Value>" + optionalFields[i].text + "</Value></SubscriberCustomField>"
}
theSoapCall += "</CustomFields>";
theSoapCall += "</Subscriber.AddWithCustomFields></SOAP-ENV:Body></SOAP-ENV:Envelope>";
// send off the soap call and get back the response from campaign monitor
var contactSendXML = new XML (theSoapCall);
var urlRequest:URLRequest = new URLRequest("http://api.createsend.com/api/api.asmx");
urlRequest.method = URLRequestMethod.POST;
urlRequest.requestHeaders.push(new URLRequestHeader("Content-Type","text/xml; charset=utf-8"));
urlRequest.requestHeaders.push(new URLRequestHeader("SOAPAction","http://api.createsend.com/api/Subscriber.AddWithCustomFields"));
urlRequest.data = contactSendXML;
urlLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.addEventListener(Event.COMPLETE, onLoaded);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, ifFailed);
urlLoader.load(urlRequest);
}
// called when campaign monitor has responded
private function onLoaded(e:Event):void {
// set the response to whatever campaign monitor returned
var recieveXML:XML = new XML(urlLoader.data);
if (recieveXML..*::Code == INVALID_EMAIL) {
trace(recieveXML..*::Message);
_response = "Invalid Email Address!";
}
else if (recieveXML..*::Code == SUCCESS) {
trace(recieveXML..*::Message);
_response = "Thank You!";
}
else if (recieveXML..*::Code == INVALID_API_KEY) {
trace(recieveXML..*::Message);
_response = "Invalid API Key";
}
else if (recieveXML..*::Code == INVALID_LIST_ID) {
trace(recieveXML..*::Message);
_response = "Invalid ListID";
}
dispatchEvent(new Event(ON_FINISHED));
}
// called if the request failed to send or campaign monitor did not respond
private function ifFailed(e:IOErrorEvent):void {
trace("Soap call could not be sent because " + e);
}
// accessor to get the response
public function get result():String {
return _response;
}
}
}
Then you need to set up some textfields named "email", "firstName", etc. etc. to match the following code in your fla:
PHP Code:
import CMRequest;
var newsletterRequest:CMRequest;
const API_ID:String = 'efiahdfy137856hsyeyrhkfs';
const LIST_ID:String = '327987528klahjksdagsausdji73';
submitBtn.addEventListener(MouseEvent.CLICK, submit);
function submit(e:MouseEvent):void {
if(email.text == '' || firstName.text == '' || lastName.text == '' || email.text == ''){
msg.text = 'EMPTY FIELD DETECTED';
return;
}
newsletterRequest = new CRMRequest(API_ID, LIST_ID, firstName, lastName, email);
newsletterRequest.addEventListener(CRMRequest.ON_FINISHED, getResponse);
}
function getResponse(e:Event):void {
msg.text = newsletterRequest.result;
}
So when you click submitBtn, it calls the submit function, which checks for any empty fields, then passes all those fields to the request and handles everything else for you. When everything is done it gives you an update in the msg textfield.