[Fixed]-Multiple forms that call same function

1👍

Your getting all the .data-submission-id elements in the dom restrict it to using jquery’s this

$('.accept_submission').on('submit', function(event){
    event.preventDefault();
    var data_post = { submission_id : $(this).find('.data-submission-id').val() }
    execute_ajax_post(data_post);
});

function execute_ajax_post(data_post) {
    var csrftoken = getCookie('csrftoken');
    $.ajaxSetup({
        beforeSend: function(xhr, settings) {
            if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
                xhr.setRequestHeader("X-CSRFToken", csrftoken);
            }
        }
    });
    $.ajax({
        url : $('.url-data').val(),
        type : "POST",
        data : data_post,
        success : function() {
            console.log("success");
        },
    });
};

P.s you could also just use the forms action rather than the .url-data input

Edit:
You could also look at using formData meaning you wouldn’t need to even get the cookie

Leave a comment