Skip to content

Instantly share code, notes, and snippets.

@yvesvanbroekhoven
Created May 4, 2018 09:58
Show Gist options
  • Save yvesvanbroekhoven/bcb5a0addbfcddf42b13181c9ee471c5 to your computer and use it in GitHub Desktop.
Save yvesvanbroekhoven/bcb5a0addbfcddf42b13181c9ee471c5 to your computer and use it in GitHub Desktop.
jQuery Campaign Monitor Ajax form
(function($) {
var pluginName = 'cmAjaxForm';
$.fn.cmAjaxForm = function(callback) {
this.each(function() {
var $this = $(this);
if (!$this.data(pluginName)) {
$this.data(pluginName, new cmAjaxForm($this, callback));
}
});
};
function cmAjaxForm($el, callback) {
this.$el = $el;
this.callback = callback;
this.formId = $el.attr('data-id');
this.bind();
}
cmAjaxForm.prototype.bind = function() {
this.$el.on('submit', $.proxy(this.onSubmit, this));
};
cmAjaxForm.prototype.onSubmit = function(e) {
e.preventDefault();
$.post('https://createsend.com//t/getsecuresubscribelink', this.$el.serialize() + '&data=' + this.formId)
.done($.proxy(function(subscribeUrl) {
if (subscribeUrl.indexOf('subscribeerror') > -1) {
console.warn('Failed to get secure subscribe link: ' + subscribeUrl);
}
$.post(subscribeUrl, this.$el.serialize())
.always($.proxy(function() {
if ($.isFunction(this.callback)) {
this.callback.apply(this.$el);
}
}, this));
}, this))
.fail(function() {
console.warn('Failed to get secure subscribe link');
});
};
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment