Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 491
Uday A. Navapara Avatar asked Sep 22 '14 08:09

Uday A. Navapara


3 Answers

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);
              }
           }
   });  
like image 101
Ashish Vaghasiya Avatar answered Nov 10 '22 19:11

Ashish Vaghasiya


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");
    }
});
like image 40
Arun P Johny Avatar answered Nov 10 '22 20:11

Arun P Johny


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;
      }
  }
});
like image 2
Edward Avatar answered Nov 10 '22 19:11

Edward