Is there any way to get form data from multiple forms in flask?

587 views Asked by At
@app.route('/',methods = ['POST'])
def user_rating():
    for i in form_value['Rating']:
        print(request.form.get(str(i)))
        
    return("Thank You")

I tried various methods but they are not working. Is there any way to iterate over form name and get their value Above is the code I am working on and want to get data from multiple forms with a single button. But able to get only the first value from the form.

HTML code:

       <tbody>
            {% for record in records %}
            <tr>
                {% for col in colnames %}
                <td>                 
                {% if (col != 'poster_link') and ( col != 'Rating' ) %}

                    {{ record[col] }}
                
                {% endif %}                    
                
                 <!--Movie Poster-->  
                {% if col == 'poster_link' %}
                
               <img src={{ record[col] }} style="width:150px">
                
               {% endif %}
               <!--end of Movie Poster-->  
               
                <!--form Rating-->
                {% if col == 'Rating' %}

                **<form method="post" id="FORMID" action="{{url_for('user_rating')}}">
                    <input type="text" name= {{ record[col] }} placeholder="Give Rating 1 to 5" >
                 </form>**
                 

                {% endif %}
                <!--end of form Rating-->
                </td>
                {% endfor %}
            </tr>
            {% endfor %}
            
        </tbody>
    </table>

    <button type="submit" form="FORMID" value="Submit">Submit</button>

Getting output like this

1
None
None
None
None
None
None
None
None
None

1

There are 1 answers

0
jdabtieu On

That is not possible because a user can only submit one form on a page. Whichever form is submitted is the one that gets read by your backend code. What you should do is either:

  1. Use AJAX to make a request every time the input is changed, or
  2. Only use one form

Based on your code, it appears that it will be easier to just use one form. So something like this would work:

<form method="post" id="FORMID" action="{{url_for('user_rating')}}">
    <table>
       <tbody>
            {% for record in records %}
            ...

                
                    <input type="text" name= {{ record[col] }} placeholder="Give Rating 1 to 5" >
                 

                ...
            {% endfor %}
            
        </tbody>
    </table>

    <button type="submit" form="FORMID" value="Submit">Submit</button>
</form>