I an trying to use a button in a form in my html, however, when I try to get the value of the clicked button in flask it returns none for some reason, I had the same problem in my code previously but it disappeared magically and I don't think that it will this time, this is my html
{% if x[4] == 0 %}
<form action="/click">
<button name="doneBtn" class="habits" style="background-color: {{x[2]}};" value="{{x[0]}}">
<p class="btnText content">{{x[1]}}</p>
<i class="{{x[3]}} habitIcons content"></i>
</button>
</form>
and this is the flask code
@app.route("/click")
def click():
id = request.form.get("doneBtn")
print(id)
cursor.execute("UPDATE info SET clicked = 1 WHERE id = ?", [id])
return redirect("/")
x is a dictionary cause I am in a jinja loop, and id is the value of the button (x[0])
The default method
for a form
element is GET
, which should land the form values into Flask's request.args
, not request.form
.
To use request.form
(as you should, given that your action performs a change on the server), you need <form method="POST" ...>
.
See more about HTTP methods, and HTML forms (particularly the method
and enctype
sections). Also, Flask API on request.args
and request.form
.