Skip to content

Instantly share code, notes, and snippets.

@sachinkiranti
Last active September 11, 2018 04:59
Show Gist options
  • Save sachinkiranti/8bee658c0ca48e80dac74e9cfb9783b7 to your computer and use it in GitHub Desktop.
Save sachinkiranti/8bee658c0ca48e80dac74e9cfb9783b7 to your computer and use it in GitHub Desktop.
Simple Form Helper to get form data
/**
* Project: Easy Form Helper
* Description: Simple Form Helper to get form data
* Author: Raisachin, https://raisachin.com.np
* Licence: MIT License http://opensource.org/licenses/mit-license.php
* Version: 1.0.0
*
* Usage:
* @type url/data : url = get url & data = object of form data
* @strict : if true , filter empty form data & if false will get all the form data
* @selector : you can provide own selector ID for your form
* @exceptor : Form data to exclude these form field
* Please use as you wish at your own risk.
*
* easyformhelper.init({
* selector: '#customselector',
* type: 'data',
* strict: false,
* exceptor: '_wpnonce,_token'
* });
*
* easyformhelper.init({
* type: 'data'
* }); // returns data
*
* easyformhelper.init(); // redirect to url
*
* Default:
* @type : url
* @strict : true
* @selector : #easyformhelper
*/
var easyformhelper = (function($) {
/**
* serialize and get form data
*/
function getFormData( selector, type, strict ) {
if (type === 'url')
{
return redirect( selector, type, strict, exceptor );
}
return getData(selector, type, strict, exceptor);
}
function redirect(selector, type, strict, exceptor) {
var data = getData(selector, type, strict, exceptor);
return window.location.href = [location.protocol, '//', location.host, location.pathname].join('') + '?' + data;
}
function getData(selector, type, strict, exceptor) {
exceptor = exceptor.split(',');
// return false;
var formdata = {};
if (strict === true)
{
formdata = $(selector).find(":input").filter(function () {
return $.trim(this.value).length > 0;
});
formdata = formdata.serializeArray()
var cleandata = {};
cleandata = formdata.filter( function (el){
return exceptor.indexOf( el.name ) < 0;
});
if ( type === 'data' ) {
var data = {};
$(cleandata ).each(function(index, obj){
data[obj.name] = obj.value;
});
return data;
}
console.log(cleandata);
return cleandata.serialize();
// TODO : serialize bata exceptor lai remove garne
// return $(selector).find(":input").filter(function () {
// return $.trim(this.value).length > 0
// }).serialize();
} else {
formdata = $(selector).serializeArray().filter( function (el){
return exceptor.indexOf( el.name ) < 0;
});;
if ( type === 'data' ) {
var vdata = {};
$(formdata ).each(function(index, obj){
vdata[obj.name] = obj.value;
});
return vdata;
}
return $(selector).serialize();
}
}
return {
init: function ( helper ) {
strict = typeof helper === 'undefined' ||
helper.strict === '' ||
typeof helper.strict === 'undefined' ? true : helper.strict;
type = typeof helper === 'undefined' ||
helper.type === '' ||
typeof helper.type === 'undefined' ? 'url' : helper.type;
selector = typeof helper === 'undefined' ||
helper.selector === '' ||
typeof helper.selector === 'undefined' ? '#easyformhelper' : helper.selector;
exceptor = typeof helper === 'undefined' ||
helper.exceptor === '' ||
typeof helper.exceptor === 'undefined' ? '' : helper.exceptor;
return getFormData( selector, type, strict, exceptor );
}
};
})(jQuery);
@sachinkiranti
Copy link
Author

Issue on multiple parameters ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment