Skip to content

Instantly share code, notes, and snippets.

@apeiros
Created September 9, 2014 06:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apeiros/9cb6c5a889ab183fce2f to your computer and use it in GitHub Desktop.
Save apeiros/9cb6c5a889ab183fce2f to your computer and use it in GitHub Desktop.
send a "virtual" form with values from JS
;(function($) {
// Sends a (virtual) form with values.
// Unlike an AJAX Post/Get, this actually makes the browser send the form and process the response. Which means that
// the site is being changed (or a file will be downloaded)
//
// Example usage:
// $.submitForm('/my/resource', {'someFormParam': 'andItsValue'}, {'method': 'PUT'});
//
// Notes:
// automatically gets the csrf-token from the <head> meta tags, unless formOptions contains the key 'setCSRF' with
// a false value.
// automatically sets the rendered_at field to the current time, unless formOptions contains the key 'setRenderedAt'
// with a false value.
// automatically translates 'method' formOptions to the rails convention if necessary, unless 'useRailsHTTPMethods' is
// set to a false value.
$.submitForm = function submitForm(action, formData, formOptions) {
var form, setCSRF, useRailsHTTPMethods, setRenderedAt;
if (!action) throw("Must provide an action");
form = $('<form>').attr('action', action);
formOptions = $.extend({}, formOptions); // copy to avoid mutating the argument
formData = $.extend({}, formData);
setCSRF = formOptions.hasOwnProperty('setCSRF') ? formOptions.setCSRF : true;
setRenderedAt = formOptions.hasOwnProperty('setRenderedAt') ? formOptions.setRenderedAt : true;
useRailsHTTPMethods = formOptions.hasOwnProperty('useRailsHTTPMethods') ? formOptions.useRailsHTTPMethods : true;
if (!formOptions.method) formOptions.method = 'POST';
delete formOptions.setCSRF;
delete formOptions.useRailsHTTPMethods;
// get rails' csrf token and set it as a value in the form
if (setCSRF) formData[$('meta[name=csrf-param]').attr('content')] = $('meta[name=csrf-token]').attr('content');
// set the rendered
if (setRenderedAt) formData['rendered_at'] = $('meta[http-equiv=x-rendered-at]').attr('content') || Math.round(Date.now()/1000);
// emulate http verbs (some browsers don't support anything but GET/POST, so PUT/PATCH/DELETE must be emulated
if (useRailsHTTPMethods && !(formOptions.method.toUpperCase() == 'POST' || formOptions.method.toUpperCase() == 'GET')) {
formData._method = formOptions.method.toUpperCase();
formOptions.method = 'POST';
}
// formOptions go into the form tag as attributes
if (formOptions) {
$.each(formOptions, function(key, value) {
form.attr(key, value);
});
}
// create input fields with the form data
$.each(formData, function(key, value) {
$('<input type="hidden">').attr('name', key).attr('value', value).appendTo(form);
});
form.hide().appendTo('body').submit();
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment