phpjavascriptpostdoubledouble-submit-problem

Javascript duplicate ids


Hello guys J have problem at Javascript. This is the code,

function reply_click(clicked_id) {
    var la = <? php echo json_encode($logOptions_id); ?> ;
    var mood = clicked_id;
    $.post('msg.php', {
        myne: la,
        mood: mood
    }, function (data) {
        $('#nov').html(data);
    });
    $('#postDiv').on('keydown', '#textarea', function (e) {
        if ((e.which == 13) && !event.shiftKey) {
            var textarea = $("#textarea").val();
            $.post('pst.php', {
                mibe: la,
                voot: mood,
                pst: textarea
            }, function (data) {
                var textarea = $("#textarea").val('');
            });
        }
    });
}

The problem is when I clicked on item I get clicked_id, after post message, its ok, but then once again I clicking the item, I get second id, and after post enter button it post in to database difference id from first item and second, seems duplicating values, how many times I click on different items , getting different ids and this problem is spamming my DB.


Solution

  • Every time you click the button, you add another keydown binding to #textarea. So if you click the button 5 times, then when you press Enter the keydown binding will be run 5 times.

    You should move $('#postDiv').on('keydown', '#textarea', ...) outside the function, and just do it once in the document ready handler.