Skip to content

Instantly share code, notes, and snippets.

@manfromanotherland
Last active July 30, 2021 07:05
Show Gist options
  • Star 85 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save manfromanotherland/975b08c39ab0a7a1b514 to your computer and use it in GitHub Desktop.
Save manfromanotherland/975b08c39ab0a7a1b514 to your computer and use it in GitHub Desktop.
JS: Ajax send forms using the most excellent Formspree » http://formspree.io #snippet
<form id="contact-form" action="//formspree.io/your@email.com" method="post">
<input type="text" name="Name" placeholder="Name" required>
<input type="email" name="Email" placeholder="Email" required>
<textarea name="Message" cols="30" rows="6" placeholder="Message" required></textarea>
<!-- CONFIG -->
<input class="is-hidden" type="text" name="_gotcha">
<input type="hidden" name="_subject" value="Subject">
<input type="hidden" name="_cc" value="email@cc.com">
<!-- /CONFIG -->
<input class="submit" type="submit" value="Send">
</form>
var $contactForm = $('#contact-form');
$contactForm.submit(function(e) {
e.preventDefault();
$.ajax({
url: '//formspree.io/your@email.com',
method: 'POST',
data: $(this).serialize(),
dataType: 'json',
beforeSend: function() {
$contactForm.append('<div class="alert alert--loading">Sending message…</div>');
},
success: function(data) {
$contactForm.find('.alert--loading').hide();
$contactForm.append('<div class="alert alert--success">Message sent!</div>');
},
error: function(err) {
$contactForm.find('.alert--loading').hide();
$contactForm.append('<div class="alert alert--error">Ops, there was an error.</div>');
}
});
});
@Nifled
Copy link

Nifled commented Oct 23, 2018

Aw, what a bummer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment