Advertisement
Guest User

ajax form

a guest
Jan 19th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Look at the AJAX section
  2. $('form.email-form').submit(function (e) {
  3.         // return false so form submits through jQuery rather than reloading page.
  4.         if(e.preventDefault) e.preventDefault();
  5.         else e.returnValue = false;
  6.        
  7.         var thisForm        = $(this).closest('.email-form'),
  8.             error           = 0,
  9.             originalError   = thisForm.attr('original-error'),
  10.             loadingSpinner;
  11.            
  12.         if (typeof originalError !== typeof undefined && originalError !== false) {
  13.             thisForm.find('.form-error').text(originalError);
  14.         }
  15.                
  16.  
  17.         $(thisForm).find('.validate-required').each(function(){
  18.             if($(this).val() === ''){
  19.                 $(this).addClass('field-error');
  20.                 error = 1;
  21.             }else{
  22.                 $(this).removeClass('field-error');
  23.             }
  24.         });
  25.        
  26.         $(thisForm).find('.validate-email').each(function(){
  27.             if(!(/(.+)@(.+){2,}\.(.+){2,}/.test($(this).val()))){
  28.                 $(this).addClass('field-error');
  29.                 error = 1;
  30.             }else{
  31.                 $(this).removeClass('field-error');
  32.             }
  33.         });
  34.        
  35.  
  36.         if (error === 1){
  37.             $(this).closest('.email-form').find('.form-error').fadeIn(200);
  38.         }else {
  39.             // Hide the error if one was shown
  40.             $(this).closest('.email-form').find('.form-error').fadeOut(200);
  41.             // Create a new loading spinner while hiding the submit button.
  42.             loadingSpinner = $('<div />').addClass('form-loading').insertAfter($(thisForm).find('input[type="submit"]'));
  43.             $(thisForm).find('input[type="submit"]').hide();
  44.            
  45.             jQuery.ajax({
  46.                 type: "POST",
  47.                 url: "mail/mail.php",
  48.                 data: thisForm.serialize(),
  49.                 success: function (response) {
  50.                     // Swiftmailer always sends back a number representing numner of emails sent.
  51.                     // If this is numeric (not Swift Mailer error text) AND greater than 0 then show success message.
  52.                     $(thisForm).find('.form-loading').remove();
  53.                     $(thisForm).find('input[type="submit"]').show();
  54.                     if($.isNumeric(response)){
  55.                         if(parseInt(response) > 0){
  56.                             thisForm.find('.form-success').fadeIn(1000);
  57.                             thisForm.find('.form-error').fadeOut(1000);
  58.                             setTimeout(function(){ thisForm.find('.form-success').fadeOut(500); }, 5000);
  59.                         }
  60.                     }
  61.                     // If error text was returned, put the text in the .form-error div and show it.
  62.                     else{
  63.                         // Keep the current error text in a data attribute on the form
  64.                         thisForm.find('.form-error').attr('original-error', thisForm.find('.form-error').text());
  65.                         // Show the error with the returned error text.
  66.                         thisForm.find('.form-error').text(response).fadeIn(1000);
  67.                         thisForm.find('.form-success').fadeOut(1000);
  68.                     }
  69.                 },
  70.                 error: function (errorObject, errorText, errorHTTP) {
  71.                     // Keep the current error text in a data attribute on the form
  72.                     thisForm.find('.form-error').attr('original-error', thisForm.find('.form-error').text());
  73.                     // Show the error with the returned error text.
  74.                     thisForm.find('.form-error').text(errorHTTP).fadeIn(1000);
  75.                     thisForm.find('.form-success').fadeOut(1000);
  76.                     $(thisForm).find('.form-loading').remove();
  77.                     $(thisForm).find('input[type="submit"]').show();
  78.                 }
  79.             });
  80.         }
  81.         return false;
  82.     });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement