A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: How to add a 'subscribe' button on my Flash based website

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    2

    How to add a 'subscribe' button on my Flash based website

    Can anyone please tell me how to add a 'subscribe' button on my Flash based website. The company that does the mailshots is


    http://www.campaignmonitor.com/


    and they have a specific page on subscribe buttons for Flash on


    http://github.com/campaignmonitor/fl...s2/tree/master


    Hopefully it makes more sense to you that to me. Action Script 2 I can follow. Action Script 3 and Java I never use.


    Thanks in advance

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    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.CLICKsubmit);

    function 
    submit(e:MouseEvent):void {
        if(
    email.text == '' || firstName.text == '' || lastName.text == '' || email.text == ''){
            
    msg.text 'EMPTY FIELD DETECTED';
            return;
        }

        
    newsletterRequest = new CRMRequest(API_IDLIST_IDfirstNamelastNameemail);
        
    newsletterRequest.addEventListener(CRMRequest.ON_FINISHEDgetResponse);
    }

    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.

  3. #3
    Junior Member
    Join Date
    Apr 2009
    Posts
    2
    Thank you very much... I'll try it and let you know how it goes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center