npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-form-submit

v1.1.8

Published

Submit your form data with a simple JavaScript call.

Downloads

19

Readme

Documentation

Read the official documentation.

Overview

Submits both <form/> element and FormData using the Fetch API with a single JS call. Submitting forms couldn't be easier!

Uses the Fetch API. Support for additional Fetch API options, such as form method (POST, GET), headers, CORS modes, etc. Defaults to POST.

Returns a Promise for handling response or error with then() and catch().

Features include:

  • 📃 Submit form data quickly and easily
    • Just a simple call. Submitting forms couldn't be easier!
  • ✅ Supports both <form/> element and FormData
    • Submit your forms your way.
  • 👍 Uses basic fetch API config with optional customization
    • Use the default POST configuration, or customize the call to your heart's content.

Donate

If this project helped you, please consider buying me a coffee or sponsoring me. Your support is much appreciated!

 

Table of Contents

Installation

npm i simple-form-submit

Quick Start

You can either:

  • Submit a <form/> element using SimpleFormSubmit.submitForm(actionUrl, form)
  • Submit FormData (see docs) using SimpleFormSubmit.submitFormData(actionUrl, formData). This is useful if you want to build form data manually or transform the form data before sending.

Both functions return a Promise so you can handle success/failure cases.

This library uses the Fetch API to make requests.

Example

We want to use JavaScript to send data for this simple form when it's submitted:

Example Form

<form id="example-form" onSubmit="handleSubmit(e)">
  <div>Email: <input name="email" /></div>
  <div>Message: <input name="message" /></div>
  <button>Send</button>
</form>

Submitting using a <form/> element

We can submit the <form/> element itself to any URL, like so:

import SimpleFormSubmit from "simple-form-submit";

const handleSubmit = (e) => {
  e.preventDefault();

  const formActionUrl = "https://www.example.com/form";
  const form = document.getElementById("example-form");

  SimpleFormSubmit.submitForm(formActionUrl, form)
    .then((response) => {
      console.log("Response:", response);
    })
    .catch((err) => {
      console.log("Error:", err);
    });
};

Submitting using a FormData object

You can also submit using a FormData object. This allows you to modify the form data before sending.

You can use new FormData(form) to extract the form data from an existing form. (Or, if you want, you can manually build your own FormData object.)

For example:

import FormData from "form-data";

const formActionUrl = "https://www.example.com/form";
const formData = new FormData(document.getElementById("example-form"));

// Transform the data here if you'd like.
formData.set("email", formData.get("email").toLowerCase());
formData.append("date", new Date().toString());

SimpleFormSubmit.submitFormData(formActionUrl, formData)
  .then((response) => {
    console.log("Response:", response);
  })
  .catch((err) => {
    console.log("Error:", err);
  });

Fetch Options

You can specify the method (GET or POST), mode (such as no-cors), headers, and other Fetch API options if you'd like. The default method is POST.

const formActionUrl = "https://www.example.com/form";
const form = document.getElementById("example-form");

// You can specify any Fetch API options here.
const fetchOptions = {
  // method: "GET",
  // mode: "no-cors",
  // ...
};

SimpleFormSubmit.submitForm(formActionUrl, form, fetchOptions)
  .then((response) => {
    console.log("Response:", response);
  })
  .catch((err) => {
    console.log("Error:", err);
  });

TypeScript

Type definitions have been included for TypeScript support.

Icon Attribution

Favicon by Twemoji.

Contributing

Open source software is awesome and so are you. 😎

Feel free to submit a pull request for bugs or additions, and make sure to update tests as appropriate. If you find a mistake in the docs, send a PR! Even the smallest changes help.

For major changes, open an issue first to discuss what you'd like to change.

⭐ Found It Helpful? Star It!

If you found this project helpful, let the community know by giving it a star: 👉⭐

License

See LICENSE.md.