The situation: I have page that lists a number of submissions. Next to each submission I want to add a button to accept said submission. I'm doing this via a form.
The html:
{% for submission in submissions %}
<div class="submission-container">
<div class = "col-sm-6">
{{ submission.submission_title }} </a>
</div>
<div class = "col-sm-6">
<form action="" method="post" class="accept_submission" style="float:left;">
{% csrf_token %}
<input type="hidden" class="data-submission-id" value="{{ submission.SUBMISSION_ID }}" />
<input type="hidden" class="url-data" value="{% url 'submission_accept' %}" />
<input type="submit" value="Accept" class="btn btn-info" />
</form>
</div>
</div>
{% endfor %}
the jquery:
$('.accept_submission').on('submit', function(event){
event.preventDefault();
var data_post = { submission_id : $('.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");
},
});
};
Now the problem is that I want each form to pass on the submission_id of the particular submission the button is associated with. However when I use classes all buttons pass on the submission_id of the first listed submission. If I use id="accept_submission" only the first button works. How would I get this to work correctly?
$(this).find('.data-submission-id').val()