title: Jquery Validation plugin SubmitHandler Ajax only works once
body: <p>hope you are doing well.</p>
  
  <p>So I'm doing a registration form (for admin), using jquery validation plugin and ajax call to process the data insertion. This is my form:</p>
  
  <pre><code>&lt;form id="add_user" class="form-horizontal" action="includes/process_adduser.php" role="form" method="post"&gt;
      &lt;div class="form-body"&gt;
          &lt;div class="alert alert-danger display-hide"&gt;
              &lt;button class="close" data-close="alert"&gt;&lt;/button&gt;
              You have some form errors. Please check below.
              &lt;/div&gt;
          &lt;div class="alert alert-success display-hide"&gt;
              &lt;button class="close" data-close="alert"&gt;&lt;/button&gt;
              Your form validation is successful!
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
          &lt;label class="col-md-3 control-label" for="musername"&gt;Username&lt;span class="required"&gt;* &lt;/span&gt;&lt;/label&gt;
          &lt;div class="col-md-3"&gt;
              &lt;input type="text" id="musername" name="musername" class="form-control" placeholder="Enter username"&gt; 
          &lt;/div&gt; 
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label class="col-md-3 control-label" for="mpassword"&gt;Password&lt;span class="required"&gt;                                       * &lt;/span&gt;&lt;/label&gt;
              &lt;div class="col-md-3"&gt;
                  &lt;input type="password" id="mpassword" name="mpassword" class="form-control" placeholder="Enter password"&gt; 
              &lt;/div&gt; 
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label class="col-md-3 control-label" for="memail"&gt;Email &lt;span class="required"&gt;* &lt;/span&gt;&lt;/label&gt;
              &lt;div class="col-md-3"&gt;
                  &lt;input type="email" id="memail" name="memail" class="form-control" placeholder="Enter email"&gt; 
              &lt;/div&gt; 
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label class="col-md-3 control-label" for="mname"&gt;Full name&lt;span class="required"&gt;* &lt;/span&gt;&lt;/label&gt;
              &lt;div class="col-md-3"&gt;
                  &lt;input type="text" id="mname" name="mname" class="form-control" placeholder="Enter name"&gt; 
              &lt;/div&gt; 
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label class="col-md-3 control-label" for="mtype"&gt;User type&lt;span class="required"&gt;* &lt;/span&gt;&lt;/label&gt;
              &lt;div class="col-md-3"&gt;
                  &lt;select id="mtype" name="mtype" class="form-control"&gt;
                      &lt;option value="editor"&gt;Editor&lt;/option&gt; 
                      &lt;option value="ad-admin"&gt;Ad-admin&lt;/option&gt; 
                      &lt;option value="administrator"&gt;Administrator&lt;/option&gt; 
                  &lt;/select&gt;
              &lt;/div&gt; 
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label class="control-label col-md-3"&gt;Access &lt;span class="required"&gt;* &lt;/span&gt;
              &lt;/label&gt;
              &lt;div class="col-md-4"&gt;
                  &lt;div class="checkbox-list" data-error-container="#add_user_access_error"&gt;  
                  &lt;label&gt;
                      &lt;input type="checkbox" value="AA" class="access" name="access[]"/&gt; AA &lt;/label&gt; 
                      &lt;input type="checkbox" value="BA" class="access" name="access[]"/&gt; BA &lt;/label&gt; 
  
                  &lt;/div&gt; 
                  &lt;div id="add_user_access_error"&gt;&lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;  
      &lt;div class="form-actions"&gt;
          &lt;div class="row"&gt;
              &lt;div class="col-md-offset-3 col-md-9"&gt;
                  &lt;button type="submit" class="btn green submit"&gt;Submit&lt;/button&gt;
                  &lt;button type="button" class="btn default"&gt;Cancel&lt;/button&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;/form&gt;
  </code></pre>
  
  <p>while my script for the validation:</p>
  
  <pre><code>&lt;script&gt;
  jQuery(document).ready(function() {   
  $('.submit').click(function(){
  console.log("submiiiiit");
  var form1 = $('#add_user');
  var error1 = $('.alert-danger', form1);
  var success1 = $('.alert-success', form1);
  
  $('#add_user').validate({ 
      errorElement: 'span', //default input error message container
      errorClass: 'help-block', // default input error message class
      focusInvalid: false, // do not focus the last invalid input
      rules: {
          musername: {
              required: true
          },
          mpassword: {
              required: true
          },
          memail: {
              required: true
          },
          mname: {
              required: true
          },
          usertype: {
              required: false
          },
          'access[]': {
              required: true,
              minlength: 1
          }
      },
  
      messages: { 
          musername: {
              required:  "Username is required."
          },
          mpassword: {
              required:  "Password is required."
          },
          memail: {
              required:  "Email is required."
          },
          mname: {
              required:  "Name is required."
          },
          'access[]': {
              required: "Access is required.",
              minlength: "Access is required."
          }
      },
  
      invalidHandler: function(event, validator) { //display error alert on form submit   
          success1.hide(); 
          $('.alert-danger span').text("You have some form errors. Please check below....");      
          $('.alert-danger', $('#add_user')).show();
          Metronic.scrollTo(error1, -200);
      },
  
      errorPlacement: function (error, element) { // render error placement for each input type
          if (element.parent(".input-group").size() &gt; 0) {
              error.insertAfter(element.parent(".input-group"));
          } else if (element.attr("data-error-container")) { 
              error.appendTo(element.attr("data-error-container"));
          } else if (element.parents('.radio-list').size() &gt; 0) { 
              error.appendTo(element.parents('.radio-list').attr("data-error-container"));
          } else if (element.parents('.radio-inline').size() &gt; 0) { 
              error.appendTo(element.parents('.radio-inline').attr("data-error-container"));
          } else if (element.parents('.checkbox-list').size() &gt; 0) {
              error.appendTo(element.parents('.checkbox-list').attr("data-error-container"));
          } else if (element.parents('.checkbox-inline').size() &gt; 0) { 
              error.appendTo(element.parents('.checkbox-inline').attr("data-error-container"));
          } else {
              error.insertAfter(element); // for other inputs, just perform default behavior
          }
      },
  
      highlight: function(element) { // hightlight error inputs
          $(element)
              .closest('.form-group').addClass('has-error'); // set error class to the control group
      },
  
      unhighlight: function (element) { // revert the change done by hightlight
          $(element)
              .closest('.form-group').removeClass('has-error'); // set error class to the control group
      },
  
      success: function (label) {
          label
              .closest('.form-group').removeClass('has-error'); // set success class to the control group
      },
  
      submitHandler: function (form) {
          //alert("submitting");
          var url = $(form).attr('action') ;
          var data = $(form).serialize()+'&amp;ajax_validation=1'; 
          var accessArray = $('.access:checked').map(function() { return this.value; }).get();
          var musername = $('input[name="musername"]').val();
          var mpassword = $('input[name="mpassword"]').val();
          var memail = $('input[name="memail"]').val();
          var mname = $('input[name="mname"]').val();
          var mtype = $('select[name="mtype"]').val();     
          // form validation success, call ajax form submit
          $.ajax({
            url: url,
            data: {
              access : accessArray,
              musername: musername,
              mpassword:mpassword ,
              memail:memail ,
              mname: mname,
              mtype:mtype
            },
            type: 'POST',
            success: function(result) {   
              if(result==="ok") { 
                  $("form#add_user")[0].reset();
                  $('input').prop( "checked", false );
                  $('.alert-success').text("New user has been added");     
                  $('.alert-success', $('#add_user')).show();
              } else {    
                  $('.alert-danger').text("error message here");      
                  $('.alert-danger', $('#add_user')).show();
              }                   
            }
          }); return false;
      }
  });
  
  $('#add_user').keypress(function(e) {
      if (e.which == 13) {  
          if ($('#add_user').validate().form()) { 
            $('#add_user').submit(); //form validation success, call ajax form submit
          }
          return false;
      }
    });
   });    
   });
   &lt;/script&gt;
  </code></pre>
  
  <p>Now, the validations works when user submits for the first time. The error message is displayed correctly. But, when they hit the submit again without refreshing the page, with or without editing the form, it just goes to the includes/process_adduser.php file - even worse, it skips my duplicate username validation.</p>
  
  <p>Did I miss anything in the code? Please kindly advice.</p>
  
  <p>Sorry guys this is a long post and I'm still learning. Thanks for your help.</p>