A lightweight JavaScript library to submig forms via XHR requests.
# Using npm
npm install --save xhr-form-submitter
# Using yarn
yarn add xhr-form-submitter
window.onload = function () {
var formEl = document.getElementById("myForm")
, checkboxEl = document.getElementsByName("xhr")[0]
, nameEl = document.getElementsByTagName("name")[0]
, mbErrorEl = document.getElementById("mb-error")
, mbSuccessEl = document.getElementById("mb-success")
, formToSubmit = new XHRFormSubmitter({
form: formEl
, disableOnSubmit: true
})
;
formEl.addEventListener("submit", function (e) {
if (!checkboxEl.checked) { return; }
try {
mbErrorEl.style.display = "none";
mbSuccessEl.style.display = "none";
formToSubmit.submit(function (err, data) {
var mb = null;
if (err) { mb = mbErrorEl; }
else { mb = mbSuccessEl; }
mb.innerHTML = err || data;
mb.style.display = "block";
mb.classList.remove("fadeInUp");
mb.classList.add("fadeInUp");
});
} catch (e) {
console.error(e.message);
}
e.preventDefault();
});
};
There are few ways to get help:
- Please post questions on Stack Overflow. You can open issues with questions, as long you add a link to your Stack Overflow question.
- For bug reports and feature requests, open issues. 🐛
- For direct and quick help, you can use Codementor. 🚀
A lightweigt JavaScript library that do one thing: submiting via XHR requests. This doesn't depend on jQuery!
For more information, see README file. Report bugs in the GitHub repository.
Developed with love by Ionică Bizău.
Creates an object containing the names and the values of the form inputs.
- Object The form data object (name: value)
This function disables the inputs. It's cool to be called before submiting the form.
- Object The XHRFormSubmitter instance
This function enables the inputs. It's cool to be called after receiving the response from server.
- Object The XHRFormSubmitter instance
Submit the form using a XMLHttpRequest.
- Object
submitOps
: The submit options
- Object The XHRFormSubmitter instance
Have an idea? Found a bug? See how to contribute.
I open-source almost everything I can, and I try to reply to everyone needing help using these projects. Obviously, this takes time. You can integrate and use these projects in your applications for free! You can even change the source code and redistribute (even resell it).
However, if you get some profit from this or just want to encourage me to continue creating stuff, there are few ways you can do it:
-
Starring and sharing the projects you like 🚀
-
—I love books! I will remember you after years if you buy me one. 😁 📖
-
—You can make one-time donations via PayPal. I'll probably buy a
coffeetea. 🍵 -
—Set up a recurring monthly donation and you will get interesting news about what I'm doing (things that I don't share with everyone).
-
Bitcoin—You can send me bitcoins at this address (or scanning the code below):
1P9BRsmazNQcuyTxEqveUsnf5CERdq35V6
Thanks! ❤️