how to perform some action before submit form via .ajaxForm()?
Asked Answered
M

3

8

i am using ajaxForm() frame work to send my data without reloading my page.

    $('#ReplayForm').ajaxForm({ 

      success : function(data){
             alert("Success");
       }   
    });  

now, i want to check some condition before submitting the form and if the condition is false then stop submission else continue.

is there any solution to do this work or is there any way buy which i can perform this operation. Thanks in advance.

Mixedup answered 22/9, 2014 at 8:54 Comment(0)
N
10

Yes, definatly you can handle this situation. you have to call beforesubmit method for this let see one example

$('#ReplayForm').ajaxForm({ 
         beforeSubmit : function(arr, $form, options){
             if("condition is true")
             {
                return true; //it will continue your submission.
             }
             else
             {
                               return false; //ti will stop your submission.
             }

          },
          success : function(data){
              endLoading();
              if(data.result=="success")            
              {
                  showSuccessNotification(data.notification);
              } 
              else
              {
                  showErrorNotification(data.notification);
              }
           }
   });  
Nebuchadnezzar answered 22/9, 2014 at 8:57 Comment(1)
and also add one js jquery.form.js <script type="text/javascript" src="/js/bootstrap/jquery.form.js"></script>Nebuchadnezzar
L
3

You can use the beforeSubmit option

$('#ReplayForm').ajaxForm({
    beforeSubmit: function (arr, $form, options) {
        //check your conditions and return false to prevent the form submission
        if (!valid) {
            return false;
        }
    },
    success: function (data) {
        alert("Success");
    }
});
Lamination answered 22/9, 2014 at 8:56 Comment(2)
but it will not work with safari and mobile browser to submit, it will directly submit without required field checkingInrush
this is also not working in window 8.1 phone IE browserNebuchadnezzar
M
3

Use beforeSend option in JQuery AJAX framework, if the test fails return false should do it.

$('#ReplayForm').ajaxForm({ 

  success : function(data){
         alert("Success");
  },
  beforeSend: function() {
      if(!myFunc()) {
          return false;
      }
  }
});
Madrigalist answered 22/9, 2014 at 8:58 Comment(6)
edward you try this code in mobile browser with required field? check its not work, without check plz do not answer the question, to many developer and programmer are checking your code and dependent on you ans.Inrush
Do you have a mobile solution Janak?Madrigalist
Mr. edward i am facing this mobile problem from last 1 month and not getting proper solution for ajaxForm submit without loading, its work fine but getting problem with mobile or safari browser only,(i dnt want to check every time javascript and jquery validation for required field)Inrush
and the alternative solution that i was looking to my code is spring validation while inserting dataInrush
Ok, well there is no need to get moody about it, why don't you post a question?Madrigalist
i was posted before, its fail to get proper solution, even jquery also speechless for this question...they reply me "we are fixing this bug very soon"Inrush

© 2022 - 2024 — McMap. All rights reserved.