'How to get data from one form by submit if there are several forms AJAX FLASK
I need to get data from one form by clicking the button when there are multiple forms, but I only get data from the first form. I understand why this doesn't work, but I don't understand how to make the data come from a specific form.
Picture and code down below
FLASK
@app.route('/product')
def product():
return render_template('product.html',
products=dbase.get_products_announce(),
check_authenticated=check_authenticated)
@app.route('/render_user_adding_item', methods=["POST"])
def render_user_adding_item():
data_get = request.form.to_dict(flat=True)
print(data_get)
HTML JS
{% block content %}
{{ super() }}
<div class="productlists">
{% for p in range(products | length) %}
<div class="list-products">
<div class="list-element">
<a href="{{ url_for('show_product', alias=products[p][7])}}"><img class="col-3" src="\static\images\productImg\{{products[p][8]}}" width="380" height="510"></a>
<div class="list-element-description">
<form id="{{ products[p][6] }}">
<div class="buttonbuy-2">
<button type="submit" class="buttonbuy">Купить</button>
<input type="hidden" id="order_id" value="{{products[p][0]}}">
<input type="hidden" id="user_id" value="{{current_user.get_id()}}">
</div>
</form>
</div>
</div>
</div>
{% endfor %}
</div>
<script>
$(document).ready(function() {
$('form').on('submit', function(event) {
$.ajax({
data : {
order_id : $('#order_id').val(),
user_id : $('#user_id').val()
},
type : 'POST',
url : '/render_user_adding_item'
})
event.preventDefault();
});
});
</script>
{% endblock %}
I apologize if I wrote something wrong, this is my first question
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
