subscribe.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. Script : Subscribe 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. $("#subscribeform").submit(function(e) {
  36. e.preventDefault();
  37. }).validate({
  38. rules: {
  39. /* uncomment if Name is needed */
  40. /*
  41. first_name: "required",
  42. last_name: "required",
  43. */
  44. email: {
  45. required: true,
  46. email: true
  47. }
  48. },
  49. submitHandler: function(form) {
  50. $("#js-subscribe-btn").attr("disabled", true);
  51. /*
  52. CHECK PAGE FOR REDIRECT (Thank you page)
  53. ---------------------------------------- */
  54. var redirect = $('#subscribeform').data('redirect');
  55. var phpurl = $('#subscribeform').attr('action');
  56. var noredirect = false;
  57. if (redirect == 'none' || redirect == "" || redirect == null) {
  58. noredirect = true;
  59. }
  60. $("#js-subscribe-btn").text('Please wait...');
  61. /*
  62. FETCH SUCCESS / ERROR MSG FROM HTML DATA-ATTR
  63. --------------------------------------------- */
  64. var success_msg = $('#js-subscribe-result').data('success-msg');
  65. var error_msg = $('#js-subscribe-result').data('error-msg');
  66. var dataString = $(form).serialize();
  67. /*
  68. AJAX POST
  69. --------- */
  70. $.ajax({
  71. type: "POST",
  72. data: dataString,
  73. url: phpurl,
  74. cache: false,
  75. success: function(d) {
  76. $(".form-group").removeClass("has-success");
  77. if (d == 'success') {
  78. if (noredirect) {
  79. $('#js-subscribe-btn').text(success_msg);
  80. $('#subscribeform')[0].reset();
  81. } else {
  82. window.location.href = redirect;
  83. }
  84. } else {
  85. $('#js-subscribe-btn').text(error_msg);
  86. console.log(d);
  87. }
  88. $("#js-subscribe-btn").attr("disabled", false);
  89. },
  90. error: function(d) {
  91. $('#js-subscribe-btn').text('Cannot access Server');
  92. },
  93. complete: function(d) {
  94. setTimeout(function(){
  95. $("#js-subscribe-btn").text('Subscribe Now!');
  96. $("#js-subscribe-btn").attr("disabled", false);
  97. }, 3000);
  98. }
  99. });
  100. return false;
  101. }
  102. });
  103. });