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.
- $(document).ready(function () {
-
- var form = $('form');
- var input = form.find('input[name="email"]');
- var action = form.attr("action");
- var msg = $('.message');
- var msgError404 = "Service is not available at the moment. Please check your internet connection or try again later."
- var msgError503 = "Oops. Looks like something went wrong. Please try again later."
- var msgErrorValidation = "This email address looks fake or invalid. Please enter a real email address."
- var msgErrorFormat = "Your e-mail address is incorrect. Please check it and try again."
- var msgSuccess = "Congrats! You are on list. We will inform you as soon as we finish."
- var note = form.find(".note");
- var icon = $("i.fa fa-paper-plane");
- var iconProcess = "fa fa-spinner fa-spin";
- var email = input.val();
- var iconSuccess = "fa fa-check-circle";
- var iconError = "fa fa-exclamation-circle";
- 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,}))$/;
-
- function sendMail(){
- // Test if the value of input is actually an email
- if(re.test(email)) {
- icon.removeClass();
- icon.addClass(iconProcess);
- form.removeClass("error success");
- note.show();
- $.ajax({
- type: "POST",
- url: action,
- data: email,
- dataType: "json",
- error: function(data){
- // Add error class to form
- form.addClass("error");
- note.hide();
- // Change the icon to error
- icon.removeClass();
- icon.addClass(iconError);
- // Determine the status of response and display the message
- if(data.status == 404) {
- msg.text(msgError404);
- $(this).notifyMe(
- 'bottom',
- 'error',
- 'Error 404',
- 'Service is not available at the moment. Please check your internet connection or try again later.',
- 200,
- 3500
- );
- } else {
- msg.text(msgError503);
- $(this).notifyMe(
- 'bottom',
- 'error',
- 'Validation Error',
- 'This email address looks fake or invalid. Please enter a real email address.',
- 200,
- 3500
- );
- }
- },
-
- }).done(function(data){
- // Hide note
- note.hide();
-
- if(data.status == "success") {
- // Add success class to form
- $(this).notifyMe(
- 'left',
- 'success',
- 'Got It!',
- "You'll be the First to know.",
- 400,
- 3500
- );
- form.addClass("success");
- //Change the icon to success
- icon.removeClass();
- icon.addClass(iconSuccess);
- msg.text(msgSuccess);
- } else {
- // Add error class to form
- if (data.type == "ValidationError") {
- $(this).notifyMe(
- 'bottom',
- 'error',
- 'Validation Error',
- 'This email address looks fake or invalid. Please enter a real email address.',
- 200,
- 3500
- );
- } else {
- $(this).notifyMe(
- 'bottom',
- 'error',
- '503 Error',
- 'Oops. Looks like something went wrong. Please try again later.',
- 200,
- 3500
- );
- }
- form.addClass("error");
- // Change the icon to error
- icon.removeClass();
- icon.addClass(iconError);
- }
- });
-
- } else {
- $(this).notifyMe(
- 'bottom',
- 'error',
- 'Check Your E-mail Address',
- 'Your e-mail address is incorrect. Please check it and try again.',
- 200,
- 3500
- );
- //Add error class to form
- form.addClass("error");
- // Hide note
- note.hide();
- // Change the icon to error
- icon.removeClass();
- icon.addClass(iconError);
- // Display the message
- msg.text(msgErrorFormat);
- }
-
- }
-
- form.on('submit', function(e){
- e.preventDefault();
- sendMail();
- });
-
- });