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>');
}
});
});
Copy link

ghost commented Jul 16, 2017

Maybe some are interested about my fork, I did some some tweaks and Code cleanups
https://gist.github.com/dotmagic/f5273c8768b18e13902b5e25b684c58a

@kandebonfim
Copy link

NIIIIIIICE! Thanks!

@RRandle
Copy link

RRandle commented Feb 5, 2018

Dude, THANK YOU!

@mrichman
Copy link

I just started getting this ajax error today:

{
  "error": "To prevent spam, only Gold accounts may create AJAX forms."
}

@Stan-89
Copy link

Stan-89 commented Feb 17, 2018

Same here.
Getting:

{
  "error": "To prevent spam, only Gold accounts may create AJAX forms."
}

So much for freebies I guess.

@shyd
Copy link

shyd commented Feb 18, 2018

Same here.

Sadly ajax has been disabled for new forms, see formspree/formspree#173

Currently we're seeing a lot of spam come from ajax forms, since we must allow ajax to bypass the CAPTCHA. Unfortunately the most strait forward solution is to remove this capability.
This PR makes submitting AJAX forms a gold feature.
Existing forms will continue to work, even AJAX forms that have been created by non gold users.

Copy link

ghost commented Mar 29, 2018

Thanks for the info!

Could you add the feature of the ability to customize the re-captcha page for gold members??

Thanks,

Brendan

@robson3999
Copy link

Hi,
I'm posting data via
$.post('https://formspree.io/email@domain.com', {name: name, email: email, message: message})
and it works, no 400 error 😄

@EhsanKia
Copy link

EhsanKia commented Jul 3, 2018

Maybe before you had a form before this change. As they say, old forms can still send ajax, but new forms can't.

@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