HTML表单即使值已更改仍不断向Flask发送相同数据。

huangapple go评论61阅读模式
英文:

HTML form keep sending the same data to the flask even the values are changed

问题

您的HTML和Flask代码中存在一个问题,导致只有第一行的更新按钮有效,并且发送的数据是第一行的数据。

问题是在HTML代码中,您在<form>标签内包裹了整个表格,但每一行都包含一个<form>标签,这可能导致表单数据混淆。您应该将<form>标签放在每一行的外部,以便每一行都有自己的表单。请修改您的HTML代码如下:

<table contenteditable>
    <thead>
        <tr>
            <th contenteditable="false">ID</th>
            <th contenteditable="false">Title</th>
            <th contenteditable="false">Description</th>
            <th contenteditable="false">Status</th>
            <th contenteditable="false">Deadline</th>
            <th contenteditable="false">Due Date</th>
            <th contenteditable="false">Task Type</th>
            <th contenteditable="false">Update</th>
        </tr>
    </thead>

    <tbody>
        {% for task in data %}
            <tr>
                <form action="/tasks" method="POST">
                    <td><input type="text" name="id" class="form-control" id="id" contenteditable="false" value="{{ task.id }}" readonly></td>
                    <td><input class="form-control" type="text" id="title" name="title_{{ task.id }}" value="{{ task.title }}"></td>
                    <td><input class="form-control" type="text" id="description" name="description_{{ task.id }}" value="{{ task.description }}"></td>
                    <td><input class="form-control" name="status_{{ task.id }}" id="status" value="{{ task.status }}" readonly></td>
                    <td><input class="form-control" type="text" id="creation_time" name="creation_time_{{ task.id }}" value="{{ task.creation_time }}"></td>
                    <td><input class="form-control" type="text" id="deadline" name="deadline_{{ task.id }}" value="{{ task.deadline }}"></td>
                    <td><input class="form-control" type="text" id="task_type" name="task_type_{{ task.id }}" value="{{ task.task_type }}"></td>
                    <td>
                        <button type="submit">Update Row</button>
                    </td>
                </form>
            </tr>
        {% endfor %}
    </tbody>
</table>

这将确保每一行都有自己的表单,以便数据不会混淆。希望这可以解决您的问题。

英文:

I am trying to code todo app in HTML and Flask. I tried to send dynamic table values to Flask with HTML but my HTML page keeps sending the same data even though the values are changed here is my code.

&lt;form action=&quot;/tasks&quot; method=&quot;POST&quot;&gt;
&lt;table contenteditable&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th contenteditable=&quot;false&quot;&gt;ID&lt;/th&gt;
&lt;th contenteditable=&quot;false&quot;&gt;Title&lt;/th&gt;
&lt;th contenteditable=&quot;false&quot;&gt;Description&lt;/th&gt;
&lt;th contenteditable=&quot;false&quot;&gt;Status&lt;/th&gt;
&lt;th contenteditable=&quot;false&quot;&gt;Deadline&lt;/th&gt;
&lt;th contenteditable=&quot;false&quot;&gt;Due Date&lt;/th&gt;
&lt;th contenteditable=&quot;false&quot;&gt;Task Type&lt;/th&gt;
&lt;th contenteditable=&quot;false&quot;&gt;Update&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
{% for task in data %}
&lt;tr&gt;
&lt;td&gt;&lt;input type=&quot;text&quot;  name = &quot;id&quot; class = &quot;form-control&quot; id = &quot;id&quot; contenteditable = &quot;false&quot; value=&quot;{{ task.id }}&quot; readonly&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class = &quot;form-control&quot; type=&quot;text&quot; id=&quot;title&quot; name=&quot;title_{{ task.id }}&quot; value=&quot;{{ task.title }}&quot;&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class = &quot;form-control&quot; type=&quot;text&quot; id=&quot;description&quot; name=&quot;description_{{ task.id }}&quot; value=&quot;{{ task.description }}&quot;&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class = &quot;form-control&quot; name =&quot;status_{{ task.id }}&quot; id = &quot;status&quot; value=&quot;{{ task.status }}&quot; readonly&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class = &quot;form-control&quot; type=&quot;text&quot; id=&quot;creation_time&quot; name=&quot;creation_time_{{ task.id }}&quot; value=&quot;{{ task.creation_time }}&quot;&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class = &quot;form-control&quot; type=&quot;text&quot; id=&quot;deadline&quot; name=&quot;deadline_{{ task.id }}&quot; value=&quot;{{ task.deadline }}&quot;&gt;&lt;/td&gt;
&lt;td&gt;&lt;input class = &quot;form-control&quot; type=&quot;text&quot; id=&quot;task_type&quot; name=&quot;task_type_{{ task.id }}&quot; value=&quot;{{ task.task_type }}&quot;&gt;&lt;/td&gt;
&lt;td&gt;
&lt;button type=&quot;submit&quot;&gt;Update Row&lt;/button&gt;
&lt;/form&gt;
&lt;/td&gt;
&lt;/tr&gt;
{% endfor %}
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/form&gt;

Here is my flask code

        title = request.form.get(f&#39;title_{task_id}&#39;)
description = request.form.get(f&#39;description_{task_id}&#39;)
status = request.form.get(f&#39;status_{task_id}&#39;)
creation_time = request.form.get(f&#39;creation_time_{task_id}&#39;)
deadline = request.form.get(f&#39;deadline_{task_id}&#39;)
task_type = request.form.get(f&#39;task_type_{task_id}&#39;)
print(title, file=sys.stderr)
print(description, file=sys.stderr)
print(task_id, file=sys.stderr)
print(status, file=sys.stderr)
ses_id = session[&#39;userid&#39;]
query = f&quot;&quot;&quot;
UPDATE Task
SET title=&#39;{title}&#39;, description=&#39;{description}&#39;, status=&#39;{status}&#39;, deadline=&#39;{deadline}&#39;, 
creation_time=&#39;{creation_time}&#39;,user_id=&#39;{ses_id}&#39;, 
task_type=&#39;{task_type}&#39;
WHERE id= {task_id};
&quot;&quot;&quot;
print(query, file=sys.stderr)
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute(query)
mysql.connection.commit()

In this code I only get the values from id, title, description, status, deadline etc. from the first row which is my page looks like thatHTML表单即使值已更改仍不断向Flask发送相同数据。

Only first row update button works and sends the data. second row update button does not send its data and sends first row's data only.

Why this is happening?

答案1

得分: 1

&lt;form action=&quot;/tasks&quot; method=&quot;POST&quot;&gt;
            &lt;table contenteditable&gt;
                &lt;thead&gt;
                    &lt;tr&gt;
                  ...
                    &lt;/tr&gt;
                &lt;/thead&gt;
        
                &lt;tbody&gt;
                    {% for task in data %}
                        &lt;form action=&quot;/tasks&quot; method = &quot;POST&quot;&gt;
                        &lt;tr&gt;
                            &lt;td&gt;&lt;input type=&quot;text&quot;  name = &quot;id&quot; class = &quot;form-control&quot; id = &quot;id&quot; contenteditable = &quot;false&quot; value=&quot;{{ task.id }}&quot; readonly&gt;&lt;/td&gt;
                            &lt;td&gt;&lt;input class = &quot;form-control&quot; type=&quot;text&quot; id=&quot;title&quot; name=&quot;title_{{ task.id }}&quot; value=&quot;{{ task.title }}&quot;&gt;&lt;/td&gt;
                            &lt;td&gt;&lt;input class = &quot;form-control&quot; type=&quot;text&quot; id=&quot;description&quot; name=&quot;description_{{ task.id }}&quot; value=&quot;{{ task.description }}&quot;&gt;&lt;/td&gt;
                            &lt;td&gt;&lt;input class = &quot;form-control&quot; name =&quot;status_{{ task.id }}&quot; id = &quot;status&quot; value=&quot;{{ task.status }}&quot; readonly&gt;&lt;/td&gt;
                            &lt;td&gt;&lt;input class = &quot;form-control&quot; type=&quot;text&quot; id=&quot;creation_time&quot; name=&quot;creation_time_{{ task.id }}&quot; value=&quot;{{ task.creation_time }}&quot;&gt;&lt;/td&gt;
                            &lt;td&gt;&lt;input class = &quot;form-control&quot; type=&quot;text&quot; id=&quot;deadline&quot; name=&quot;deadline_{{ task.id }}&quot; value=&quot;{{ task.deadline }}&quot;&gt;&lt;/td&gt;
                            &lt;td&gt;&lt;input class = &quot;form-control&quot; type=&quot;text&quot; id=&quot;task_type&quot; name=&quot;task_type_{{ task.id }}&quot; value=&quot;{{ task.task_type }}&quot;&gt;&lt;/td&gt;
                            &lt;td&gt;
                                &lt;button type=&quot;submit&quot;&gt;Update Row&lt;/button&gt;
                            &lt;/td&gt;
                        &lt;/tr&gt;
                        &lt;/form&gt;
                    {% endfor %}
                &lt;/tbody&gt;
            &lt;/table&gt;
        &lt;/form&gt;
英文:
&lt;form action=&quot;/tasks&quot; method=&quot;POST&quot;&gt;
            &lt;table contenteditable&gt;
                &lt;thead&gt;
                    &lt;tr&gt;
                  ...
                    &lt;/tr&gt;
                &lt;/thead&gt;
        
                &lt;tbody&gt;
                    {% for task in data %}
                        &lt;tr&gt;
                            &lt;td&gt;&lt;input type=&quot;text&quot;  name = &quot;id&quot; class = &quot;form-control&quot; id = &quot;id&quot; contenteditable = &quot;false&quot; value=&quot;{{ task.id }}&quot; readonly&gt;&lt;/td&gt;
                            &lt;td&gt;&lt;input class = &quot;form-control&quot; type=&quot;text&quot; id=&quot;title&quot; name=&quot;title_{{ task.id }}&quot; value=&quot;{{ task.title }}&quot;&gt;&lt;/td&gt;
                            &lt;td&gt;&lt;input class = &quot;form-control&quot; type=&quot;text&quot; id=&quot;description&quot; name=&quot;description_{{ task.id }}&quot; value=&quot;{{ task.description }}&quot;&gt;&lt;/td&gt;
                            &lt;td&gt;&lt;input class = &quot;form-control&quot; name =&quot;status_{{ task.id }}&quot; id = &quot;status&quot; value=&quot;{{ task.status }}&quot; readonly&gt;&lt;/td&gt;
                            &lt;td&gt;&lt;input class = &quot;form-control&quot; type=&quot;text&quot; id=&quot;creation_time&quot; name=&quot;creation_time_{{ task.id }}&quot; value=&quot;{{ task.creation_time }}&quot;&gt;&lt;/td&gt;
                            &lt;td&gt;&lt;input class = &quot;form-control&quot; type=&quot;text&quot; id=&quot;deadline&quot; name=&quot;deadline_{{ task.id }}&quot; value=&quot;{{ task.deadline }}&quot;&gt;&lt;/td&gt;
                            &lt;td&gt;&lt;input class = &quot;form-control&quot; type=&quot;text&quot; id=&quot;task_type&quot; name=&quot;task_type_{{ task.id }}&quot; value=&quot;{{ task.task_type }}&quot;&gt;&lt;/td&gt;
                            &lt;td&gt;
                                &lt;button type=&quot;submit&quot;&gt;Update Row&lt;/button&gt;
                            &lt;/form&gt;
                            &lt;/td&gt;
                        &lt;/tr&gt;
                    {% endfor %}
                &lt;/tbody&gt;
            &lt;/table&gt;
        &lt;/form&gt;

Looking at the html code you only initialized a form tag once,

However the first iteration of

{% for task in data %}

includes a closing tag for the form, therefore terminating the scope of the form to only include the data from the first iteration (or the first row).

It's also redundant that you have another </form> at the end of the code.
One of the approach is to encapsulate every column into a form such as

 {% for task in data %}

&lt;form action=&quot;/tasks&quot; method = &quot;POST&quot;&gt;
                        &lt;tr&gt;
                            &lt;td&gt;&lt;input type=&quot;text&quot;  name = &quot;id&quot; class = &quot;form-control&quot; id = &quot;id&quot; contenteditable = &quot;false&quot; value=&quot;{{ task.id }}&quot; readonly&gt;&lt;/td&gt;
                            &lt;td&gt;&lt;input class = &quot;form-control&quot; type=&quot;text&quot; id=&quot;title&quot; name=&quot;title_{{ task.id }}&quot; value=&quot;{{ task.title }}&quot;&gt;&lt;/td&gt;
                            &lt;td&gt;&lt;input class = &quot;form-control&quot; type=&quot;text&quot; id=&quot;description&quot; name=&quot;description_{{ task.id }}&quot; value=&quot;{{ task.description }}&quot;&gt;&lt;/td&gt;
                            &lt;td&gt;&lt;input class = &quot;form-control&quot; name =&quot;status_{{ task.id }}&quot; id = &quot;status&quot; value=&quot;{{ task.status }}&quot; readonly&gt;&lt;/td&gt;
                            &lt;td&gt;&lt;input class = &quot;form-control&quot; type=&quot;text&quot; id=&quot;creation_time&quot; name=&quot;creation_time_{{ task.id }}&quot; value=&quot;{{ task.creation_time }}&quot;&gt;&lt;/td&gt;
                            &lt;td&gt;&lt;input class = &quot;form-control&quot; type=&quot;text&quot; id=&quot;deadline&quot; name=&quot;deadline_{{ task.id }}&quot; value=&quot;{{ task.deadline }}&quot;&gt;&lt;/td&gt;
                            &lt;td&gt;&lt;input class = &quot;form-control&quot; type=&quot;text&quot; id=&quot;task_type&quot; name=&quot;task_type_{{ task.id }}&quot; value=&quot;{{ task.task_type }}&quot;&gt;&lt;/td&gt;
                            &lt;td&gt;
                                &lt;button type=&quot;submit&quot;&gt;Update Row&lt;/button&gt;
                            &lt;/form&gt;
                            &lt;/td&gt;
                        &lt;/tr&gt;
                    {% endfor %}

huangapple
  • 本文由 发表于 2023年4月6日 23:44:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定