/* Author: Amol Nirmala Waman Author URL: http://blog.navayan.com Plugin URL: http://cdn.navayan.com/public/libraries/jquery.plugins.js Copyright: (C) Author, Copyright details must be intact with this plugin jQuery Plugins: 1. ResetFormFields -> USAGE: $("form#xyz").ResetFormFields(); 2. AjaxFormPost -> USAGE: HTML:
JAVASCRIPT: $('form#xyz').AjaxFormPost({ ActionUrl: 'includes/form-val-invitation.php' }); OR VARIABLE BASED METHOD $('#submit').click(function(e){ e.preventDefault(); AjaxSubmit( $(this), 'includes/ajax.php?type=SubmitContact', true, '' ); // true = use ResetFormFields, '' = extra callback }); PHP: return array varible - $return['err'], $return['msg'] */ if (typeof jQuery != undefined) { jQuery(function ($) { $.fn.ResetFormFields = function () { this.each(function () { var i = '', thisForm = $(this), typesHTML4 = ['text', 'password'], typesHTML5 = ['range', 'number', 'tel', 'color', 'search', 'email', 'url', 'date', 'time', 'datetime', 'datetime-local', 'month', 'week'], types = typesHTML4.concat(typesHTML5), typesLength = types.length; $('select, textarea', thisForm).val(''); $('input[type="radio"], input[type="checkbox"]', thisForm).attr('checked', false); for (i = 0; i < typesLength; i++) { $('input[type="' + types[i] + '"]', thisForm).val(''); } // WYSIWYG reset $('.mce-tinymce iframe body', thisForm).html(''); }); } // INC/DECREASE INT VAL $.fn.NumIncDec = function (options) { $(this).on('keypress', function(e){ var val = parseInt( $(this).val() ), key = (e.charCode) ? e.charCode: ((e.keyCode) ? e.keyCode:((e.which)?e.which:0)), defaults = { min : 0, max : 9 }, o = $.extend(defaults, options); if (key == 38 && val < o.max) { val += 1; }else if (key == 40 && val > o.min) { val -= 1; }else{ return false; } $(this).val(val); }); }; // AJAX POST WITHOUT REAL TIME PARAM SUPPORT $.fn.AjaxFormPost = function (options) { var thisForm = $(this), defaults = { ActionUrl: thisForm.attr('action'), // default will take 'action' attribute value ResponseEle: $('.AS-Res', thisForm), // response element holds message element MessageEle: $('.AS-Msg', thisForm), // actual message will print in this element ProcessEle: $('.AS-Process', thisForm), // loader/process/progress element which shows a processing behaviour ProcessTxt: 'Processing...', PostSuccess: function(){} }, o = $.extend(defaults, options); o.ProcessEle.hide(); o.ResponseEle.hide(); o.MessageEle.removeClass('AS-Success').removeClass('AS-Error').text(''); $('input[type="submit"]', thisForm).click(function(e){ e.preventDefault(); o.ProcessEle.show(); o.ResponseEle.show(); o.MessageEle.empty().text(o.ProcessTxt).removeClass('AS-Success').addClass('AS-Error'); $.ajax({ type: 'post', url: o.ActionUrl, data: thisForm.serialize(), dataType: 'json', fail: function (response) { o.ProcessEle.hide(0); o.MessageEle.html(response.msg); //if (response) thisForm.PageScroll(); }, error: function (response) { o.ProcessEle.hide(0); o.MessageEle.html(response.msg); //if (response) thisForm.PageScroll(); }, success: function (response) { // Object.keys(response)[0]; o.ProcessEle.hide(0); if (response.err){ o.MessageEle.html(response.msg); }else{ thisForm.ResetFormFields(); o.MessageEle.html(response.msg).removeClass('AS-Error').addClass('AS-Success'); o.PostSuccess; } //if (response) thisForm.PageScroll(); } }); }); } // AJAX SUBMIT WITH REAL TIME PARAM SUPPORT $.fn.AjaxSubmitForm = function (options) { $(this).live('click', function(e){ e.preventDefault(); var SubmitBtn = $(this), SubmitForm = SubmitBtn.parents('form'), Flag = false, defaults = { ActionUrl : SubmitForm.attr('action'), // default will take 'action' attribute value ActionConcat: '', ResponseEle: $('.AS-Res', SubmitForm), // response element holds message element MessageEle: $('.AS-Msg', SubmitForm), // actual message will print in this element ProcessEle: $('.AS-Process', SubmitForm), // loader/process/progress element which shows a processing behaviour ProcessTxt: 'Processing...', BeforeSubmit: function(){}, AfterSuccess: function(){}, ClearForm: true, ConfirmBox: false, ConfirmTxt: '' }, o = $.extend(defaults, options); if (o.ConfirmBox) { if(window.confirm(o.ConfirmTxt)){ Flag = true; } }else{ Flag = true; } if (Flag) { o.BeforeSubmit.call(this); SubmitBtn.attr('disabled', 'disabled'); o.ProcessEle.css('display', 'inline-block'); o.ResponseEle.show().removeClass('AS-Success').addClass('AS-Error'); $('.fa', o.ResponseEle).show(); o.MessageEle.empty().text(o.ProcessTxt); $.ajax({ type: 'post', url: o.ActionUrl + o.ActionConcat, data: SubmitForm.serialize(), dataType: 'json', //async: false, cache: false, //contentType: false, //processData: false, fail: function (response) { o.ProcessEle.hide(0); o.MessageEle.html(response.msg); SubmitBtn.removeAttr('disabled'); //if (response) thisForm.PageScroll(); }, error: function (response) { o.ProcessEle.hide(0); o.MessageEle.html(response.msg); SubmitBtn.removeAttr('disabled'); //if (response) thisForm.PageScroll(); }, success: function (response) { // Object.keys(response)[0]; $('.fa', o.ResponseEle).hide(); if (response.err){ o.MessageEle.html(response.msg); }else{ if(o.ClearForm){ try { SubmitForm.ResetFormFields(); // WYSIWYG reset $('.mce-tinymce iframe body', SubmitForm).text('').html(''); } catch(e) { console.log(e); } } o.ResponseEle.removeClass('AS-Error').addClass('AS-Success'); o.MessageEle.html(response.msg); o.AfterSuccess.call(this); } o.ProcessEle.hide(0); SubmitBtn.removeAttr('disabled'); //if (response) thisForm.PageScroll(); } }); } }); }; // AjaxSubmitForm() // DELETE SINGLE RECORD $.fn.AjaxDeleteRecord = function (options) { $(this).live('click', function(e){ //debugger; e.preventDefault(); var i = $(this), rel = i.attr('rel'), Flag = false, defaults = { ActionUrl : this.href, HashVal: this.hash.slice(1), ToRemove: i.closest('tr'), ConfirmBox: false, ConfirmTxt: "Are you sure to delete ", ConfirmTxtConcat: (rel ? rel : ''), ProcessTxt: 'Deleting...', AfterSuccess: function(){} }, o = $.extend(defaults, options); if (o.ConfirmBox) { if(window.confirm(o.ConfirmTxt + o.ConfirmTxtConcat + '?')){ Flag = true; } }else{ Flag = true; } if (Flag) { i.text(o.ProcessTxt); $.ajax({ type: 'post', dataType: 'json', url: o.ActionUrl + o.HashVal, fail: function( response ) { alert( response.msg ); }, error: function( response ) { alert( response.msg ); }, success: function( response ){ if (response.err){ alert( response.msg ); }else{ o.ToRemove.remove(); o.AfterSuccess.call(this); } } }); } }); }; // AjaxDeleteRecord() AjaxSubmit = function (ASBtn, ASUrl, ClearForm, AfterComplete) { var ASForm = ASBtn.parents('form'), ResponseEle= $('.AS-Res', ASForm), // response element holds message element MessageEle = $('.AS-Msg', ASForm), // actual message will print in this element ProcessEle = $('.AS-Process', ASForm), // loader/process/progress element which shows a processing behaviour ProcessTxt = 'Processing...'; ASBtn.attr('disabled', 'disabled'); ProcessEle.css('display', 'inline-block'); ResponseEle.show().removeClass('AS-Success').addClass('AS-Error'); MessageEle.text('').text(ProcessTxt); $.ajax({ type: 'post', url: ASUrl, data: ASForm.serialize(), dataType: 'json', fail: function (response) { ProcessEle.hide(0); MessageEle.text(response.msg); ASBtn.removeAttr('disabled'); //if (response) thisForm.PageScroll(); }, error: function (response) { ProcessEle.hide(0); MessageEle.text(response.msg); ASBtn.removeAttr('disabled'); //if (response) thisForm.PageScroll(); }, success: function (response) { // Object.keys(response)[0]; if (response.err){ MessageEle.text(response.msg); }else{ if(ClearForm) ASForm.ResetFormFields(); ResponseEle.removeClass('AS-Error').addClass('AS-Success'); MessageEle.text(response.msg); setTimeout(function(){ AfterComplete; }, 300); } ProcessEle.hide(0); ASBtn.removeAttr('disabled'); //if (response) thisForm.PageScroll(); } }); } $.fn.PageScroll = function (options) { var defaults = { diff : 0 }, o = $.extend(defaults, options), im = $(this); //console.log(im); $('html,body').animate({ scrollTop: im.offset().top - o.diff }, 500); //$(this).offset().top, $(this).position().top //$('html,body').scrollTo(this); // } // AJAX SUBMIT WITH REAL TIME PARAM SUPPORT AjaxSubmit = function (ASBtn, ASUrl, ClearForm, AfterComplete) { var ASForm = ASBtn.parents('form'), ResponseEle= $('.AS-Res', ASForm), // response element holds message element MessageEle = $('.AS-Msg', ASForm), // actual message will print in this element ProcessEle = $('.AS-Process', ASForm), // loader/process/progress element which shows a processing behaviour ProcessTxt = 'Processing...'; ASBtn.attr('disabled', 'disabled'); ProcessEle.css('display', 'inline-block'); ResponseEle.show().removeClass('AS-Success').addClass('AS-Error'); MessageEle.text('').text(ProcessTxt); $.ajax({ type: 'post', url: ASUrl, data: ASForm.serialize(), dataType: 'json', fail: function (response) { ProcessEle.hide(0); MessageEle.text(response.msg); ASBtn.removeAttr('disabled'); //if (response) thisForm.PageScroll(); }, error: function (response) { ProcessEle.hide(0); MessageEle.text(response.msg); ASBtn.removeAttr('disabled'); //if (response) thisForm.PageScroll(); }, success: function (response) { // Object.keys(response)[0]; if (response.err){ MessageEle.text(response.msg); }else{ if(ClearForm) ASForm.ResetFormFields(); ResponseEle.removeClass('AS-Error').addClass('AS-Success'); MessageEle.text(response.msg); setTimeout(function(){ AfterComplete; }, 300); } ProcessEle.hide(0); ASBtn.removeAttr('disabled'); //if (response) thisForm.PageScroll(); } }); } valEmail = function (field){ var f = field, fval = f.val(); with (f){ if(fval != ''){ apos = fval.indexOf('@'); dotpos = fval.lastIndexOf('.'); if (apos < 1 || dotpos-apos < 2){ f.css('color', '#f00'); return false;} else{f.css('color', 'inherit'); return true;} } } } remainChars = function ( me, maxChars, target ){ var meVal = me.val().trim() || me.text().trim(), meLen = meVal.length, remain= parseInt(maxChars - meLen); if ( meLen > maxChars ){ me.val( (meVal).substring(0, meLen - 1) ); }else{ target.text(remain); } } }); }