Skip to main content

Basic Form

Add a form to any Hugo template or content file:
layouts/partials/contact-form.html
<form action="https://spike.ac/api/f/YOUR_FORM_SLUG" method="POST">
  <input type="hidden" name="_next" value="{{ .Permalink }}thank-you/">
  <input type="text" name="_gotcha" style="display:none">
  
  <div>
    <label for="name">Name</label>
    <input type="text" id="name" name="name" required>
  </div>
  
  <div>
    <label for="email">Email</label>
    <input type="email" id="email" name="email" required>
  </div>
  
  <div>
    <label for="message">Message</label>
    <textarea id="message" name="message" required></textarea>
  </div>
  
  <button type="submit">Send Message</button>
</form>

Shortcode

Create a reusable shortcode:
layouts/shortcodes/contact.html
{{ $formSlug := .Get "slug" }}
<form action="https://spike.ac/api/f/{{ $formSlug }}" method="POST">
  <input type="text" name="_gotcha" style="display:none">
  <input type="email" name="email" placeholder="Email" required>
  <textarea name="message" placeholder="Message" required></textarea>
  <button type="submit">Send</button>
</form>
Use in content:
{{</* contact slug="YOUR_FORM_SLUG" */>}}

With Config

Store your form slug in config.toml:
config.toml
[params]
  spikeFormSlug = "YOUR_FORM_SLUG"
layouts/partials/contact-form.html
<form action="https://spike.ac/api/f/{{ .Site.Params.spikeFormSlug }}" method="POST">
  <input type="text" name="_gotcha" style="display:none">
  
  <input type="text" name="name" required>
  <input type="email" name="email" required>
  <textarea name="message" required></textarea>
  
  <button type="submit">Send</button>
</form>

AJAX Submission

Add JavaScript for submission without page reload:
layouts/partials/ajax-form.html
<form id="contact-form" action="https://spike.ac/api/f/{{ .Site.Params.spikeFormSlug }}" method="POST">
  <input type="text" name="_gotcha" style="display:none">
  
  <input type="email" name="email" required>
  <textarea name="message" required></textarea>
  
  <button type="submit">Send</button>
  <p id="form-status"></p>
</form>

<script>
document.getElementById('contact-form').addEventListener('submit', async function(e) {
  e.preventDefault();
  const form = this;
  const status = document.getElementById('form-status');
  
  status.textContent = 'Sending...';
  
  try {
    const res = await fetch(form.action, {
      method: 'POST',
      body: new FormData(form),
      headers: { 'Accept': 'application/json' }
    });
    
    if (res.ok) {
      status.textContent = 'Thanks!';
      form.reset();
    } else {
      status.textContent = 'Error, please try again.';
    }
  } catch (err) {
    status.textContent = 'Error, please try again.';
  }
});
</script>