Debug School

rakesh kumar
rakesh kumar

Posted on

How to prevent to insert multiple duplicate record in database using jquery

If you're experiencing issues with multiple duplicate records being inserted when pressing the "Add" button quickly, it could be due to the asynchronous nature of JavaScript and the server-side processing of the requests. To handle such scenarios, you may consider implementing a mechanism to prevent multiple simultaneous requests for the same action.
when i click add button quickly i face this issue

Image description

function addSocialSites() {
    // Check if another request is already in progress
    if (isAdding) {
        return;
    }

    // Set the flag to indicate that a request is in progress
    isAdding = true;

    // Perform your AJAX request or other operations here
    $.ajax({
        url: 'your-api-endpoint',
        method: 'POST',
        // other options...
        success: function(response) {
            // Handle the response as needed
            console.log(response);
        },
        error: function(error) {
            // Handle errors
            console.error(error);
        },
        complete: function() {
            // Reset the flag when the request is complete, allowing the next request
            isAdding = false;
        }
    });
}
Enter fullscreen mode Exit fullscreen mode
  let isAdding = false;
    function submitSocialUrlAjax(url) {
    // Check if another request is already in progress
    if (isAdding) {
        return;
    }

    // Set the flag to indicate that a request is in progress

        console.log('submitSocialUrlAjax Faceboo button pe click ho rha hai');
    // Use the URL to make the AJAX call


    $.ajax({

        url: "{{ url('addprofile/social-url') }}",
        type: "POST",
        data: new FormData($('#addProfile_Sample_Form')[0]),
        contentType: false,
        cache: false,
        processData: false,
        dataType: "json",
        headers: {
            "Authorization": "Bearer " + localStorage.getItem('a_u_a_b_t')
        },
        success: function (data) {
            var html = '';
            if (data.success) {
                html = '<div class="alert alert-success">' + data.message +
                    '</div>';
                $('#store_url_result').html(html);
                setTimeout(function () {
                    $('#addProfileModal').modal('hide');
                    location.reload(true);
                }, 2000);
            } else {
                html = '<div class="alert alert-danger">' + data.message +
                    '</div>';
                $('#store_url_result').html(html);
            }
        },
        error: function (data) {
            console.log('Error:', data);
            console.log('Update function kamm nahi kr rha hai');
        }
    });
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)