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

spemailhandler

v1.5.0

Published

A script to handle the interaction between a Bootstrap-based contact form and the server, handling form submission and feedback.

Downloads

345

Readme

SPEmailHandler - Contact Form Script

SPEmailHandler is a simple client-side contact form handler. It manages form validation, form submissions, and user feedback for server response errors via JavaScript.

Installation

Install SPEmailHandler from npm:

npm install spemailhandler

Then, in your JavaScript:

import { ContactForm } from "spemailhandler";

const formHandler = new ContactForm("/path/to/your-server-script.php");

HTML Structure

The following HTML structure is required for SPEmailHandler to function correctly. Be sure to use the correct id and class attributes as the script depends on these identifiers.

Form Fields:

Name Input:

<input type="text" id="name" placeholder="Your Name" />

Email Input:

<input type="email" id="email" placeholder="Your Email" />

Message Input:

<textarea id="message" placeholder="Your Message"></textarea>

Submit Button:

<button id="sendmessage">Send Message</button>

Cancel Button (for modal)

<button id="contactCancel" data-bs-dismiss="modal">Cancel</button>

Alert and Modal

Alert for Displaying Errors

<div id="message-alert" style="display: none;"></div>

Success Modal

<div
  class="modal fade"
  id="success"
  tabindex="-1"
  aria-labelledby="successLabel"
  aria-hidden="true"
>
  Success Message
</div>

How to Use

Initialize the Contact Form

After including the necessary HTML elements, initialize ContactForm by passing the URL of your server-side script that handles form submissions.

const formHandler = new ContactForm("/path/to/your-server-script.php");

Optionally, include:

  • CSRF Token Input Name: If your server requires a CSRF token, specify the name attribute of the token input field.
  • onSuccess Callback: A function to handle successful submissions (e.g., updating the UI or logging data).
const formHandler = new ContactForm(
  "/path/to/your-server-script.php",
  "csrf_token", // Optional CSRF token input name
  (responseData) => {
    console.log("Form submitted successfully:", responseData);
  }, // Optional callback function
);

Form Validation and Submission

  • Email Validation: The script validates email format before submission.
  • SPEmailHandler: Both the email and message fields must be filled out. If not, an alert displays prompting users to complete the fields.

Error Handling

  • If there is an issue with the server response, a default error message is displayed in #message-alert.
  • You can customize the error message by modifying the server-side response.

CSRF Token Support

If your server requires a CSRF token, ensure the corresponding hidden input field exists in your HTML:

<input type="hidden" name="csrf_token" value="your-csrf-token" />

Server-Side Script Requirements

The server script should return a JSON response in the following format:

{
  "status": "success", // or "error"
  "message": "Optional error message"
}

Example

<form id="contact-form">
  <input type="text" id="name" placeholder="Your Name" />
  <input type="email" id="email" placeholder="Your Email" />
  <textarea id="message" placeholder="Your Message"></textarea>
  <div id="message-alert" style="display: none;"></div>
  <button id="sendmessage">Send Message</button>
</form>

<div
  class="modal fade"
  id="success"
  tabindex="-1"
  aria-labelledby="successLabel"
  aria-hidden="true"
>
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="successLabel">Success</h5>
        <button
          type="button"
          class="btn-close"
          data-bs-dismiss="modal"
          aria-label="Close"
        ></button>
      </div>
      <div class="modal-body">Your message has been sent successfully!</div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
          Close
        </button>
      </div>
    </div>
  </div>
</div>