Skip to content

Instantly share code, notes, and snippets.

@Ulv
Created January 12, 2015 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ulv/97a6aa5138a6ad96ea40 to your computer and use it in GitHub Desktop.
Save Ulv/97a6aa5138a6ad96ea40 to your computer and use it in GitHub Desktop.
js modal dialog: send mail, countdown and redirect
/*
* form mail + модальное окно при сабмите
*/
$.fn.extend({
diplomMailForm: function(data) {
countdown = function (element, seconds, minutes) {
var interval;
minutes = minutes || 0;
seconds = seconds || 5;
return interval = setInterval(function () {
var el;
el = document.getElementById(element);
if (seconds === 0) {
if (minutes === 0) {
clearInterval(interval);
window.location.href = "/";
return;
} else {
minutes--;
seconds = 60;
}
}
el.innerHTML = seconds;
seconds--;
}, 1000);
};
return this.each(function() {
var self, typeIsArray;
self = this;
typeIsArray = Array.isArray || function(value) {
return {}.toString.call(value) === '[object Array]';
};
return $(this).on('submit', function() {
var labels, lbl, txt, _i, _len, _ref;
labels = {};
_ref = $(this).find('input, select, textarea');
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
lbl = _ref[_i];
txt = $(lbl).attr('name');
if (txt) {
txt = txt.replace(/^form\[/g, "");
labels[txt] = $(lbl).siblings('label').html() || $(lbl).parent().siblings('label').html() || $(lbl).attr("placeholder");
}
}
$.post('/send.php', {
data: $(this).serialize(),
labels: labels
}, function(r) {
if (r.status && r.status === 'success') {
$('#modalDlg').show();
return countdown('countdown', 4);
}
});
return false;
});
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment