Skip to content

Instantly share code, notes, and snippets.

@jamesmusgrave
Last active October 3, 2018 06:03
Show Gist options
  • Save jamesmusgrave/895e62b84308df91a38fa023cbf02a2a to your computer and use it in GitHub Desktop.
Save jamesmusgrave/895e62b84308df91a38fa023cbf02a2a to your computer and use it in GitHub Desktop.
Angular method to handle Campaign Monitor's secure subscribe link signup
<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 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