Form Submit from a WebPart SharePoint

Unfortunately a form element can’t exist within a web part. The form element already exists in the master page of the web part.

To resolve the issue the the jquery file jquery.SharePointFormSubmit.js file can be downloaded from https://sharepointformsubmit.codeplex.com/ to facilitate form submit from within the web part.  Instead of the real HTML, use a div and a button with the submit event attached.

However the script had to be modified slightly to work according to blog post http://sadomovalex.blogspot.co.uk/2011/01/submit-html-form-using-content-editor.html

The code has been modified further to remove added ids to the input fields by SharePoint when the fields are rendered on the UI.

(function ($) {
$.fn.SharePointFormSubmit = function (element, method, action) {
var e; // new input element
var f = document.createElement("form"); // form
f.method = method;
f.action = action;
f.setAttribute("style", "display:none");
$(element).find("input, select, textarea").each(function () {
e = document.createElement("input")
e.setAttribute("type", $(this).attr("type"));
//id and name are modified when rendered on UI, use the delimitor txt_ to retrieve correct id and name
var inputId = $(this).attr("id").split("txt_");
if (inputId.length > 1) {
e.setAttribute("id", inputId[1]);
}
var inputName = $(this).attr("name").split("txt_");
if (inputName.length > 1) {
e.setAttribute("name", inputName[1]);
//alert(inputName[1]);
}
//e.setAttribute("name", $(this).attr("name"));
e.setAttribute("value", $(this).val());
f.appendChild(e);
});
var s = document.body.appendChild(f);
s.submit();
};
})(jQuery);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s