-1

I use a jquery post to send data to a simple php page:


$( "body" ).on( "keyup", ".attribuzioneabbonamento", function(){
    var AbbonamentoCodice = $(this).val();
    var IdRiga = $(this).attr('dd');

        if(AbbonamentoCodice.length == 13){

                $.post("engine.php",
                {
                    Action: 'attribuisciabbonamento',
                    Code: AbbonamentoCodice,
                    IdRiga: IdRiga
                },

                function(data, status){
                    if(status == 'success'){
                        $('.abbonamentorisposta'+IdRiga).html(data);

                    }else{

                    }

              });


        }
}); 

It works, but the problem is that every time the post is executed 2 times.

Why is it called 2 times?

Minal Chauhan
  • 6,025
  • 8
  • 21
  • 41
  • 1
    Presumably your `keyup` event is firing twice. Or you have other code not shown here which is causing the problem. Or perhaps you've made a mistake or false assumption in your debugging. Can you clarify? – David May 26 '18 at 11:50
  • 1
    How are you determining the post occurs twice vs something occurring twice server side using one post? – charlietfl May 26 '18 at 11:51
  • i know is called 2 times return status change first call
    success
    after 1 "micro" second print
    fail
    – Nicola Lorenzini May 26 '18 at 12:15

1 Answers1

1

Try this function. May be you are binding the same function twice somehow. So unbind the previous bound function with off()

The $( "body" ).off("input.myinput") will unbind only your bound keyup function and not any other keyup events bound on body.

 $( "body" ).off("input.mykeyup").on( "input.myinput", ".attribuzioneabbonamento", function(){
    var AbbonamentoCodice = $(this).val();
    var IdRiga = $(this).attr('dd');

        if(AbbonamentoCodice.length == 13){

                $.post("engine.php",
                {
                    Action: 'attribuisciabbonamento',
                    Code: AbbonamentoCodice,
                    IdRiga: IdRiga
                },

                function(data, status){
                    if(status == 'success'){
                        $('.abbonamentorisposta'+IdRiga).html(data);

                    }else{

                    }

              });


        }
}); 
Nandita Sharma
  • 13,287
  • 2
  • 22
  • 35