Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

373
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!