Python 3.9 Django 3.2 I have categories and product , and through a simple form in the template I choose 1 or 2-3 checkboxes and I get the result. How to do it now via ajax and without button 'submit'.
I select several product categories and each click on the checkbox is ajax and the next click on the checkbox adds the parameter to the request.
it is similar to how you select several parameters in online stores. You have selected 2 parameters and received a filtered list of products
I suppose that you need to intercept the sending, store one request in the cache and add a new one to this request
<form method="post" id="category-box">
{% csrf_token %}
<div class="form-check">
<input class="form-check-input" type="checkbox" name='category' value="{{c.name}}" >
<label class="form-check-label" for="{{c.name}}"><a href="{{ c.get_absolute_url }}">{{ c.name }}</a></label>
</div>
{% endfor %}
<button type="submit">Submit</button>
</form>
You can have an onChange on the form element which triggers when any input within the form has been changed. When the form changes, you send AJAX request with updated params and load new data.
This might be hurtful for performance though, clicking multiple checkboxes in short period of time would trigger a request for each click which is unnecessary. That can be handled with a slight delay before sending the request and a check if the form has changed subsequently.