1

So I was looking how to validate before AJAX executes. My AJAX was works fine before I add if ($("#ModalsForm").validate()){}. After I add that validate, it will validate and return true and send Get to my index.php.

index.php?bookingid=10040917035610&roomid=10&...

My HTML

<form id="ModalsForm" method="post" data-parsley-excluded="[disabled=disabled]">
     <div id="myModal" class="modal fade" 
     tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display:none;">
         <div class="modal-dialog modal-lg">
            <div class="modal-content" id="modalcontent">
            <!-- AJAX onclick content modal -->
            </div><!-- /.modal-content -->
         </div><!-- /.modal-dialog -->
     </div>
 </form>

My AJAX

function ConfirmSubmit() {
  var x = confirm("Are you sure you want to save or update?");
  if (x) {
    if ($("#ModalsForm").validate()) {
      e.preventDefault();
      $.ajax({
        type: "POST",
        url: "modules/BookingCalendar/runsave.php",
        data: $("#ModalsForm").serialize(),
        contentType: "application/x-www-form-urlencoded",
        success: function(result) {
          alert(result);
          //$("#information").html(result);
          $('#myModal').modal('toggle');
        }
      });
      return false;
    } else {
      return false;
    }
  } else {
    return false;
  }
}

Did I miss something when I validate my form?

<script>
$('#ModalsForm').validate({
  submitHandler: function() {
    if (confirm("Are you sure you want to save or update?")) {
      $.ajax({
        type: "POST",
        url: "modules/BookingCalendar/runsave.php",
        data: $("#ModalsForm").serialize(),
        contentType: "application/x-www-form-urlencoded",
        success: function (result) {
          alert(result);
          $('#myModal').modal('toggle'); 
        }
      });
      return false;
    }
  }
});
</script>

Newest From Rory McCrossan, it's caught error in console

1
  • e.preventDefault(): e is not defined anywhere. Did you check the console for errors? Commented Sep 5, 2017 at 8:21

1 Answer 1

3

If you're using jQuery validate and want to submit the form via AJAX you should invert the logic, so that you use the submitHandler option of the plugin to implement your own logic when the form is valid, like this:

$('#ModalsForm').validate({
  rules: { /* your rules. .*/ },
  messages: { /* your messages... */ },
  submitHandler: function() {
    if (confirm("Are you sure you want to save or update?")) {
      $.ajax({
        type: "POST",
        url: "modules/BookingCalendar/runsave.php",
        data: $("#ModalsForm").serialize(),
        contentType: "application/x-www-form-urlencoded",
        success: function (result) {
          $('#myModal').modal('toggle'); 
        }
      });
    }
    return false;
  }
});

You can then remove the ConfirmSubmit() function, and just submit the form element normally.

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

6 Comments

so required inside input is useless when i use ajax ?
No, you can still use that method too, this was just for an example
hm.. validation is work, but my ajax not even working after i try to use submitHandler: function() {} and it submit just like there is no script i think
Apologies - I missed the return false;. I updated the answer for you
wait i will update my code, because it's skip the script
|

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.