0

Submit button does not work after an ajax call; however, if I load the page it works fine. This is what is going on: I do have a modal that shows a dialog concerning email change confirmation. If the user clicks confirm, then the form submits fine; however, if the user clicks no and go ahead and submits the form with the originally saved email address, but updates different forms, the button does not submits anything. I would have to reload the page again to make it work. Here is the JS code for the above behavior.

==> In a nutshell, what I am trying to do is as follows: If the customer comes and tries to change their email address, the modal pops up with a confirmation message (Yes or no). If they click yes then the form submits fine; however, if they click no and reenter their original email, the submit button does not submit the form.

$(document).ready(function() {

  window.onload = function() {

    document.getElementById('confirmEmailChange').onclick = function() {
      document.getElementById('updateCustomerInfoFormId').submit();
      return false;
    };
    $('#updateInfoBut').click(function() {

      let currentCustomerEmail = $('#currentCustomerEmail').val();
      let customerEmail = $('#customerEmail').val();

      if (currentCustomerEmail !== customerEmail) {

        $('form[name=update_customer]').submit(function(e) {
          e.preventDefault();
        });

        $.ajax({
          url: "This is our API URL" + customerEmail,
          dataType: 'text',
          success: function(json) {

          },
          statusCode: {
            404: function() {
              $('#customerInfoModal').modal({
                show: true
              });

            },
            200: function() {
              $('#emailAlreadyUsedModal').modal({
                show: true
              });
            }
          }
        });
      }
    });
  }
});
2
  • Have you checked the console log for errors ? It's possible an error has been encountered, which is preventing the button from working. Commented Sep 19, 2019 at 17:14
  • @devlincarnate I do not get any errors in the console! Commented Sep 19, 2019 at 17:18

2 Answers 2

1

It looks like

$('form[name=update_customer]').submit(function(e) {
    e.preventDefault();
});

is preventing the form from submit itself via preventDefault() call.

The default action of a form submit is to submit the form itself, by prevent the default action you basically tells the form to submit then stop it from submitting.

If you look at here, you will find that passing a callback to submit() essentially gives it a handler rather than submit the form. And the handler will be called every time when you try to submit the form in other ways.

Sign up to request clarification or add additional context in comments.

8 Comments

Yes, I need that behavior to check for other validations. I later on submit the form through other button clicks...
@Zeusox Because when you intercept the submit function, you are not only trigger this one but also adding the callback to the form submit. If you add a line of log before the preventDefault() call, you should see it get triggered everytime you are trying to submit the form, either via button click or other methods.
@Zeusox - Putting e.preventDefault() there makes that part of the code pointless. As @Chen says your form is never sent.
It depends on what you are trying to do. If you are trying to send the form via ajax, leave the preventDefault and move your ajax call inside the submit event.
@Zeusox May be you can validate the form manually instead of calling the submit?
|
0

I have figured this out by moving the e.preventDefault(); inside the if statement function, and manually submitting the button outside the ajax call when the modal is confirmed. The following works great:

   $(document).ready(function(){

  document.getElementById('confirmEmailChange').onclick = function() {
      document.getElementById('updateCustomerInfoFormId').submit();
      return false;
  };

  $('#updateInfoBut').on('click', function(e) {
    let currentCustomerEmail = $('#currentCustomerEmail').val();
    let customerEmail = $('#customerEmail').val();
        if(currentCustomerEmail !== customerEmail){
          e.preventDefault();
          $.ajax({
               url:"API LINK GOES HERE" + customerEmail,
               dataType: 'text',
               success:function(json){
               },
               statusCode: {
               404: function() {
                 $('#customerInfoModal').modal({show: true});

               },
               200: function() {
                 $('#emailAlreadyUsedModal').modal({show: true});

               }
                }
          });

        } 
  });

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.