Looking for some assistance to help steer me in the right direction. My professor is unable to provide guidance. I'm currently struggling with handling POST requests in my form for select2 multiple fields. The issue arises from a Yes/No flag that determines the display of select options.
The javascript code below effectively manages the show/hide functionality:
$(document).ready(function () {
$('#option').change(function () {
$this = $(this)
$('.select_box').each(function () {
$select = $('select', this);
if ($select.data('id') == $this.val()) {
$(this).show();
$select.show().select2();
} else {
$(this).hide();
$('select', this).hide();
}
});
});
});
Within my form, there's a choice between Yes/No/NA which triggers the appropriate select 2 box with the provided javascript above.
<select name ="option" class="form-control " id="option">
<option value="1"> Yes</option>
<option value="0"> No</option>
<option value="2"> N/A</option>
</select>
To handle the form POST request, the following standard POST method is employed:
<form action = "{% url 'final' %}" form method = "POST">
Inside the form, there's the .select_box div class where the select 2 option accurately displays the fields and populates the multi-select feature as expected.
<div id = "div_yesselect" class="select_box">
<script src= "{% static '/accounts/option_select2.js' %}" type="text/javascript"></script>
<select data-placeholder="Please select your course(s)?" data-id="1" class="js-example-basic-multiple" multiple="multiple" id = "id_courseselect" value = "{{course.id}}" style="width: 1110px" >
{% for course in courses%}
<option value="{{course.name}}" name = "courseid" >{{course.name}}</option>
{% endfor %}
</select>
Upon hitting the Submit Button, the POST request is processed. However, when users select multiple or single options, only the values of other fields are validated, not the select2 option.
<button class="btn btn-primary " type="submit">Submit</button>
In my final view, attempting to retrieve the POST data for the select2 name results in an empty set, even though all other form fields return the correct values. Why does the select2 option fail to post correctly?
courselist = request.POST.getlist('courseid')