jquery - php archive are be called more than once -


well, have jquery post:

$('.form-submit').click(function(){      var form = $(this).attr('data-form');      $("#"+form).submit(function(e) {          e.preventdefault();         $.ajax({             url: "../assets/action/action.php",             type: "post",             data: new formdata(this),             contenttype: false,             cache: false,             processdata:false,             success: function(data)             {                    $(".return-post-msg").html(data);             }         });     }); }) 

the thing is, working fine, if click button.form-submit again, jquery called once action.php file called twice, , if click again, 3x... if clcik againx.. have in action.php

echo "<script> alert('".$method." aaaaa'); </script>";  

for check how many times file being called...

here html:

            <form id="create_requisite">                  <input type="hidden" name="method" value="create_requisite" />                  <div class="modal-heading"> <!-- start of modal-heading -->                      <h3>create requisite</h3>                  </div> <!-- end of modal-heading -->                  <div class="modal-body"> <!-- start of modal-body -->                      <div class="form-group"> <!-- start of form-group -->                          <label>requisite name</label>                         <input type="text" class="form-control input-primary" name="name_requisite" />                      </div> <!-- end of form-group -->                  </div> <!-- end of modal-body -->                  <div class="modal-footer"> <!-- start of modal-footer -->                      <span class="return-post-msg"></span>                     <button class="btn btn-default form-submit" data-form="create_requisite">create</button>                  </div> <!-- end of modal-footer -->              </form> 

can me?

you're listening both click event , submit event causing event fire multiple times. instead, listen submit event.

$(this).attr('data-form').on('submit', function(e) {   // make ajax call }); 

you can use .one() while declaring event handler ever handle event once.

$(this).attr('data-form').one('submit', function(e) {   // make ajax call }); 

however, use otherwise users may need refresh page can fix on form after try submit.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -