contact.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. Script : Contact Form
  3. Version : 1.0
  4. Author : Surjith S M
  5. URI : http://themeforest.net/user/surjithctly
  6. Copyright © All rights Reserved
  7. Surjith S M / @surjithctly
  8. */
  9. $(function() {
  10. "use strict";
  11. /* ================================================
  12. jQuery Validate - Reset Defaults
  13. ================================================ */
  14. $.validator.setDefaults({
  15. ignore: [],
  16. highlight: function(element) {
  17. $(element).closest('.form-group').addClass('has-error');
  18. },
  19. unhighlight: function(element) {
  20. $(element).closest('.form-group').removeClass('has-error');
  21. },
  22. errorElement: 'small',
  23. errorClass: 'help-block',
  24. errorPlacement: function(error, element) {
  25. if (element.parent('.input-group').length || element.parent('label').length) {
  26. error.insertAfter(element.parent());
  27. } else {
  28. error.insertAfter(element);
  29. }
  30. }
  31. });
  32. /*
  33. VALIDATE
  34. -------- */
  35. $("#contact_form").submit(function(e) {
  36. e.preventDefault();
  37. }).validate({
  38. rules: {
  39. name: "required",
  40. email: {
  41. required: true,
  42. email: true
  43. }
  44. },
  45. submitHandler: function(form) {
  46. $("#js-contact-btn").attr("disabled", true);
  47. /*
  48. CHECK PAGE FOR REDIRECT (Thank you page)
  49. ---------------------------------------- */
  50. var redirect = $('#contact_form').data('redirect');
  51. var phpurl = $('#contact_form').attr('action');
  52. var noredirect = false;
  53. if (redirect == 'none' || redirect == "" || redirect == null) {
  54. noredirect = true;
  55. }
  56. $("#js-contact-btn").attr("disabled", true);
  57. $('#js-contact-result').fadeIn('slow').html('<div class="error-msg">Please wait</div>');
  58. /*
  59. FETCH SUCCESS / ERROR MSG FROM HTML DATA-ATTR
  60. --------------------------------------------- */
  61. var success_msg = $('#js-contact-result').data('success-msg');
  62. var error_msg = $('#js-contact-result').data('error-msg');
  63. var dataString = $(form).serialize();
  64. /*
  65. AJAX POST
  66. --------- */
  67. $.ajax({
  68. type: "POST",
  69. data: dataString,
  70. url: phpurl,
  71. cache: false,
  72. success: function(d) {
  73. $(".form-group").removeClass("has-success");
  74. if (d == 'success') {
  75. if (noredirect) {
  76. $('#js-contact-result').fadeIn('slow').html('<div class="alert alert-success error-msg">' + success_msg + '</div>').delay(3000).fadeOut('slow');
  77. $('#contact_form')[0].reset();
  78. } else {
  79. window.location.href = redirect;
  80. }
  81. } else {
  82. $('#js-contact-result').fadeIn('slow').html('<div class="alert alert-danger error-msg">' + error_msg + '</div>').delay(3000).fadeOut('slow');
  83. }
  84. setTimeout(function(){
  85. $("#js-contact-btn").attr("disabled", false);
  86. }, 1000);
  87. },
  88. error: function(d) {
  89. $('#js-contact-result').fadeIn('slow').html('<div class="alert alert-danger error-msg"> Cannot access Server </div>').delay(3000).fadeOut('slow');
  90. setTimeout(function(){
  91. $("#js-contact-btn").attr("disabled", false);
  92. }, 1000);
  93. }
  94. });
  95. return false;
  96. }
  97. });
  98. })