Ajax with php only working in console

Ajax with php only working in console

It's just email, why so difficult??? I have a really simple form, only the email address as an input. The form action is mail.php. I'm using a plugin script notifyme.js for some nice visuals and an ajax call to handle everything. I fire ajax on form 'submit'.  Problem is I can't get ajax to run! My code runs in the console when using xammp on my local machine. I can see the response and the status is 200. But when I refresh and just click the button to submit the form all I can get are my error messages, and only the default message for when somethings wrong but I don't know why. When running from my hosting server I get the same thing. Any ideas???

My code is wraped in a document.ready(function) I'm think it has something to do with they way jQuery is defined $ vs JQuery, but I'm really not sure. Would love some help!

Thanks.


  1. $(document).ready(function () {

  2. var form = $('form');
  3. var input = form.find('input[name="email"]');
  4. var action = form.attr("action");
  5. var msg = $('.message');
  6. var msgError404 = "Service is not available at the moment. Please check your internet connection or try again later."
  7. var msgError503 = "Oops. Looks like something went wrong. Please try again later."
  8. var msgErrorValidation = "This email address looks fake or invalid. Please enter a real email address."
  9. var msgErrorFormat = "Your e-mail address is incorrect. Please check it and try again."
  10. var msgSuccess = "Congrats! You are on list. We will inform you as soon as we finish."
  11. var note = form.find(".note");
  12. var icon = $("i.fa fa-paper-plane");
  13. var iconProcess = "fa fa-spinner fa-spin";
  14. var email = input.val();
  15. var iconSuccess = "fa fa-check-circle";
  16. var iconError = "fa fa-exclamation-circle"; 
  17. var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

  18. function sendMail(){
  19. // Test if the value of input is actually an email
  20. if(re.test(email)) {
  21. icon.removeClass();
  22. icon.addClass(iconProcess);
  23. form.removeClass("error success");
  24. note.show();
  25. $.ajax({
  26. type: "POST",
  27. url: action,
  28. data: email,
  29. dataType: "json",
  30. error: function(data){
  31. // Add error class to form
  32. form.addClass("error");
  33. note.hide();
  34. // Change the icon to error
  35. icon.removeClass();
  36. icon.addClass(iconError);
  37. // Determine the status of response and display the message
  38. if(data.status == 404) {
  39. msg.text(msgError404);
  40. $(this).notifyMe(
  41. 'bottom',
  42. 'error',
  43. 'Error 404',
  44. 'Service is not available at the moment. Please check your internet connection or try again later.',
  45. 200,
  46. 3500
  47. );
  48. } else {
  49. msg.text(msgError503);
  50. $(this).notifyMe(
  51. 'bottom',
  52. 'error',
  53. 'Validation Error',
  54. 'This email address looks fake or invalid. Please enter a real email address.',
  55. 200,
  56. 3500
  57. );
  58. }
  59. },
  60. }).done(function(data){
  61. // Hide note
  62. note.hide();
  63. if(data.status == "success") {
  64. // Add success class to form
  65. $(this).notifyMe(
  66. 'left',
  67. 'success',
  68. 'Got It!',
  69. "You'll be the First to know.",
  70. 400,
  71. 3500
  72. );
  73. form.addClass("success");
  74. //Change the icon to success
  75. icon.removeClass();
  76. icon.addClass(iconSuccess);
  77. msg.text(msgSuccess);
  78. } else {
  79. // Add error class to form
  80. if (data.type == "ValidationError") {
  81. $(this).notifyMe(
  82. 'bottom',
  83. 'error',
  84. 'Validation Error',
  85. 'This email address looks fake or invalid. Please enter a real email address.',
  86. 200,
  87. 3500
  88. );
  89. } else {
  90. $(this).notifyMe(
  91. 'bottom',
  92. 'error',
  93. '503 Error',
  94. 'Oops. Looks like something went wrong. Please try again later.',
  95. 200,
  96. 3500
  97. );
  98. }
  99. form.addClass("error");
  100. // Change the icon to error
  101. icon.removeClass();
  102. icon.addClass(iconError);
  103. }
  104. });
  105. } else {
  106. $(this).notifyMe(
  107. 'bottom',
  108. 'error',
  109. 'Check Your E-mail Address',
  110. 'Your e-mail address is incorrect. Please check it and try again.',
  111. 200,
  112. 3500
  113. );
  114. //Add error class to form
  115. form.addClass("error");
  116. // Hide note
  117. note.hide();
  118. // Change the icon to error
  119. icon.removeClass();
  120. icon.addClass(iconError);
  121. // Display the message
  122. msg.text(msgErrorFormat);
  123. }
  124. }
  125. form.on('submit', function(e){
  126. e.preventDefault();
  127. sendMail();
  128. });
  129. });