Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i avoid duplicate request of button click twice

i am clicking a button and calling a function... when i click button twice the

data is inserting twice

in mysql database how can i avoivd it in my case..?

This my dynamic html:

<button type="button"
    href="javascript:;"
     class="btn btn-primary"
       onclick="jobResponse('yes',<?php echo $myjobs['id_job'];?>)"> 
         Subimt </button>

This is my function with ajax call:

 <script type="text/javascript">
    function jobResponse(res,jobId){
        var frm_data = { res : res,
            jobId : jobId
        }
        $.ajax({
            //ajax resopons and requst
        });
    }
    </script>

How can i resolve this issue..?

like image 871
Lynda Carter Avatar asked Dec 03 '22 23:12

Lynda Carter


1 Answers

Pass the button reference and disable it and later when ajax is completed enable it again if needed.

<button type="button" href="javascript:;" class="btn btn-primary" onclick="jobResponse(this, 'yes',<?php echo $myjobs['id_job'];?>)">    Subimt</button>
<!--                                                                   ----------------^^^^----


<script type="text/javascript">
  function jobResponse(ele, res, jobId) {
    var frm_data = {
      res: res,
      jobId: jobId
    };
    // disable the button
    $(ele).prop('disabled', true);
    $.ajax({
      //ajax resopons and requst
      complete: function() {
        // enable the button if you need to
        $(ele).prop('disabled', false);
      }
    });
  }
</script>
like image 77
Pranav C Balan Avatar answered Dec 11 '22 16:12

Pranav C Balan