Last active
October 3, 2018 06:03
-
-
Save jamesmusgrave/895e62b84308df91a38fa023cbf02a2a to your computer and use it in GitHub Desktop.
Angular method to handle Campaign Monitor's secure subscribe link signup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form name="newsletterSignupForm" novalidate ng-submit='submitEmail()'> | |
<input ng-model="emailAddress" type="email" placeholder="[email protected]" required /> | |
<div class="checkbox"> | |
<input type="checkbox" ng-model="acceptTerms" name="acceptTerms" required /> | |
<div class="checkbox-label">I’ve read and accept the <a href="/terms" target="_blank">TOS and Privacy Policy</a>. </div> | |
</div> | |
<button ng-disabled="!newsletterSignupForm.$valid" type="submit" ng-bind="subscribeText"></button> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is the long data-id from the form element | |
var id = ''; | |
// This is the name of the email input element | |
var email_name = 'cm-'; | |
var submitEmailPartTwo = function(secureUrl, email){ | |
$http({ | |
method: 'POST', | |
url: secureUrl, | |
headers: { | |
"Content-type" : "application/x-www-form-urlencoded" | |
}, | |
transformRequest: function() { | |
return email_name+"="+email; | |
} | |
}).then(function successCallback(response) { | |
$scope.subscribeText = 'Thanks!'; | |
$timeout(function() { | |
$scope.subscribeText = 'Subscribe'; | |
}, 5000); | |
}, function errorCallback(response) { | |
$scope.subscribeText = 'Error!'; | |
$timeout(function() { | |
$scope.subscribeText = 'Subscribe'; | |
}, 5000); | |
}); | |
} | |
var submitEmailPartOne = function(email){ | |
$http({ | |
method: 'POST', | |
url: 'https://createsend.com//t/getsecuresubscribelink', | |
headers: { | |
"Content-type" : "application/x-www-form-urlencoded" | |
}, | |
transformRequest: function(obj) { | |
var str = []; | |
for(var p in obj){ | |
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); | |
} | |
return str.join("&"); | |
}, | |
data: { | |
'email': email, | |
'data': id | |
} | |
}).then(function successCallback(response) { | |
submitEmailPartTwo(response.data, email); | |
}, function errorCallback(response) { | |
$scope.subscribeText = 'Error!'; | |
$timeout(function() { | |
$scope.subscribeText = 'Subscribe'; | |
}, 5000); | |
}); | |
} | |
$scope.submitEmail = function() { | |
$scope.subscribeText = 'Please Wait...'; | |
submitEmailPartOne($scope.emailAddress); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment