Materialize Contact Request Ajax Form

Contact form that doesn't require page refresh. jQuery >= v1.6 example.
<div class="container">
  <div class="row">
    <div class="col s12 m8 offset-m2 offset-l3 l7 offset-xl4 xl6">
      <form id="contact-form" method="POST" action="https://herotofu.com/start">
        <div class="row">
          <div class="input-field col s12">
            <input
              name="name"
              placeholder="Joe Bloggs"
              id="full_name"
              type="text"
              class="validate"
            />
            <label for="full_name">Your name</label>
          </div>
        </div>
        <div class="row">
          <div class="input-field col s12">
            <input
              name="email"
              placeholder="joe.bloggs@example.com"
              id="email_address"
              type="email"
              class="validate"
              required
            />
            <label for="email_address">Email address</label>
          </div>
        </div>

        <div class="row">
          <div class="input-field col s12">
            <textarea
              name="message"
              id="message"
              class="materialize-textarea validate"
              placeholder="Tell us what you're thinking about..."
            ></textarea>
            <label for="message">Message</label>
          </div>
        </div>

        <div class="row">
          <div class="col s12">
            <button
              id="form-submit"
              type="submit"
              class="btn waves-effect waves-light"
            >
              Contact Us
            </button>
          </div>
        </div>

        <div class="right-align">
          <small
            >by
            <a
              href="https://herotofu.com"
              class="grey-text text-darken-3"
              target="_blank"
              >HeroTofu</a
            ></small
          >
        </div>
      </form>
    </div>
  </div>
</div>
<script>
  $('#contact-form').submit(function (e) {
    e.preventDefault();

    var $form = $(this),
      $formSubmit = $('#form-submit'),
      formArray = $form.serializeArray(),
      data = {};

    $formSubmit.attr('disabled', 'disabled').text('Loading...');

    for (var i = 0; i < formArray.length; i++) {
      data[formArray[i].name] = formArray[i].value;
    }

    $.ajax({
      type: 'POST',
      contentType: 'application/json; charset=utf-8',
      dataType: 'json',
      url: $form.attr('action'),
      data: JSON.stringify(data),
    }).always(function (data) {
      if (data.status === 422) {
        alert('Ajax requests do not support captcha. Please turn it off.');
        $formSubmit
          .parent()
          .html('<strong>Error, failed to send the message!</strong>');
      }

      if (data.status !== 200) {
        alert('Error: ' + data.statusText + ' (' + data.status + ')');
        $formSubmit
          .parent()
          .html('<strong>Error, failed to send the message!</strong>');
      }

      $formSubmit.parent().html('Thank you for contacting us!');
    });
  });
</script>
/* The following CSS is used only for the preview,
   please don't copy it to your production code */
body { padding: 2rem 0; }

Frequently Asked Questions

Can I use it freely in a commercial project?

Yes! Attribution is welcomed but not required. The only caveat is that you can't re-sell this code directly as a standalone component.

Why is it free?

It's free because HeroTofu gets revenue from form submissions handling rather than forms themselves.

I've copied the form code. What are the next steps?

You should connect your form to the backend to store submissions. Subjectively the best way to do so is to navigate to https://herotofu.com/start 🙂

Who's behind HeroTofu?

Munero Company operates HeroTofu. It's incorporated in the EU 🇪🇺

HeroTofu is a set of tools and APIs designed to help Developers and Marketers.

© 2024 HeroTofu by Munero