Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

381
Visualizações
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

html forms and ajax code

flask code

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

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda