skip to Main Content

onkeyup ajax post twice to sql
twice data to sql
insert data to sql twice redcord

function myFunction() {
             var x = $("#number_2").val();
               if(x.length === 2){
                      $.ajax({
                      type: "POST",
                      url: "insert2.php",
                      data: {message: x},
                      success: function(data) {
                       $('#number_2').val('')
                      }
                      });                    
               }
             }
<input type="text" id="number_2" onkeyup="myFunction()" class="text-center fast form-control" >

2

Answers


  1. Chosen as BEST ANSWER

    This code works.

    $(document).ready(function () {
                $("#number_2").on("keyup", function (e) {
                    // alert($("#number_2"));
                // let isSubmitProcess = false;
                
                //     isSubmitProcess = true;
                //     if (isSubmitProcess) {
                //      return;
                //     }
                    e.preventDefault();
                    var x = $("#number_2").val();
                    if (x.length === 2) {
                        $.ajax({
                            type: "POST",
                            url: "insert2.php",
                            data: { message: x },
                            success: function (data) {
                                // $('#number_2').val('');
                                // isSubmitProcess = false;
                            }
                        });
                    }
                    if (x.length > 1) {
                        $('#number_2').val('');
                    }
                    
                });
            });
    <input type="text" id="number_2" class="text-center fast form-control" />


  2. HTML

    <input type="text" id="number_2" class="text-center fast form-control" />
    

    JavaScript

    $(document).ready(function () {
        let isSubmitProcess = false;
        $("#number_2").on("keyup", function (e) {
            isSubmitProcess = true;
            if (isSubmitProcess) {
             return;
            }
            e.preventDefault();
            var x = $("#number_2").val();
            if (x.length === 2) {
                $.ajax({
                    type: "POST",
                    url: "insert2.php",
                    data: { message: x },
                    success: function (data) {
                        $('#number_2').val('');
                        isSubmitProcess = false;
                    }
                });
            }
        });
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search