简体   繁体   中英

JavaScript/ Php no response after email submit button

Please could you help me I have the below javascript code everything works fine email validating works fine but when a visitor types in there email address and click sign up button the confirmation message 'You will be notified with our latest events!' doesn't show up.

  $(document).ready(function(){
 //Bind JavaScript event on SignUp Button
    $('#submitbtn').click(function(){
        signUp($('#email').val());
    }); 

var signUp = function(inputEmail)
{
    var isValid = true;
    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    if(!emailReg.test(inputEmail)){
        isValid = false;
        alert('Your email is not in valid format');
    }
    if(isValid){
        var params = {
            'action'    : 'SignUp',
            'email'     : inputEmail
        };
        $.ajax({
            type: "POST",
            url: "scripts/mail.php",
            data: params,
            success: function(response){
                if(response){
                    var responseObj = jQuery.parseJSON(response);
                    if(responseObj.ResponseData)
                    {
                        $('#submitbtn').val('');
                        showMessage('You will be notified with our latest events!');

                    }
                }
            }
        });
    }
};

  var mousedownHappened = false;

  $("#submitbtn").mousedown(function() {
    mousedownHappened = true;
  });

  $("#email").focus(function(){
    $(this).animate({
      opacity: 1.0,
      width: '250px'
    }, 300, function(){
      // callback method empty
    });

    // display submit button
    $("#submitbtn").fadeIn(300);
  });


  $("#email").blur(function(){
    if(mousedownHappened) {
      // reset mousedown and cancel fading effect
      mousedownHappened = false;

    } else {
      $("#email").animate({
        opacity: 0.75,
        width: '250px'
      }, 500, function(){
        // callback method empty
      });

      // hide submit button
      $("#submitbtn").fadeOut(400);
    }
  });
});

As @Mike said you should add a failure handler to know if there was an error:

$.ajax({
        type: "POST",
        url: "scripts/mail.php",
        data: params,
        success: function(response){
            if(response){
                var responseObj = jQuery.parseJSON(response);
                if(responseObj.ResponseData)
                {
                    $('#submitbtn').val('');
                    showMessage('You will be notified with our latest events!');

                }
            }
        },

        error: function(response){ 
          showMessage('Sorry, there was an error saving you email. :(');
        }

    });

Edit: you have to add a ',' after close success function.

You have only an on-success handler. You need an error handler here. It looks like your AJAX call is actually going through, but coming back as an error and the $.ajax call has no way of reporting it. Read down to 'error' here to see what the error handler should look like. You should also set a break point at the start of your success and error handlers to make sure that at least one of them is called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM