- 
      
- 
        Save JWizerd/c3de26fadec4142097032fe040469a34 to your computer and use it in GitHub Desktop. 
    JS: Ajax send forms using the most excellent Formspree » http://formspree.io #snippet
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <form id="contact-form" action="//formspree.io/[email protected]" 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 protected]"> | |
| <!-- /CONFIG --> | |
| <input class="submit" type="submit" value="Send"> | |
| </form> | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | var $contactForm = $('#contact-form'); | |
| $contactForm.submit(function(e) { | |
| e.preventDefault(); | |
| var $submit = $('input:submit', $contactForm); | |
| var defaultSubmitText = $submit.val(); | |
| $.ajax({ | |
| url: '//formspree.io/[email protected]', | |
| method: 'POST', | |
| data: $(this).serialize(), | |
| dataType: 'json', | |
| beforeSend: function() { | |
| //$contactForm.append('<div class="alert alert--loading">Sending message…</div>'); | |
| $submit.attr('disabled', true).val('Sending message…'); | |
| }, | |
| success: function(data) { | |
| //$contactForm.append('<div class="alert alert--success">Message sent!</div>'); | |
| $submit.val('Message sent!'); | |
| setTimeout(function() { | |
| //$('.alert--success').remove(); | |
| $submit.attr('disabled', false).val(defaultSubmitText); | |
| }, 5000); | |
| }, | |
| error: function(err) { | |
| //$contactForm.find('.alert--loading').hide(); | |
| //$contactForm.append('<div class="alert alert--error">Ops, there was an error.</div>'); | |
| $submit.val('Ops, there was an error.'); | |
| setTimeout(function() { | |
| //$('.alert--error').remove(); | |
| $submit.attr('disabled', false).val(defaultSubmitText); | |
| }, 5000); | |
| } | |
| }); | |
| }); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment