I'm trying to get the value from a hidden input field in a form. The page has 4 forms, and each form has a different value for the hidden input field - each for a different quarter of the year.
<form method="get" action="{{ url_for('index') }}">
<input type="hidden" name="value" value="{{1}}">
<button >1st Quarter</button>
</form>
<form method="get" action="{{ url_for('index') }}">
<input type="hidden" name="value" value="{{2}}">
<button >2nd Quarter</button>
</form>
<form method="get" action="{{ url_for('index') }}">
<input type="hidden" name="value" value="{{3}}">
<button >3rd Quarter</button>
</form>
<form method="get" action="{{ url_for('index') }}">
<input type="hidden" name="value" value="{{4}}">
<button >4th Quarter</button>
</form>
The python looks like this:
@app.route("/", methods=["GET", "POST"])
def index():
sales_data = Sales.query.all()
if request.method == "GET":
quarter = request.form.get("name")
print(quarter)
return render_template("index.html", sales_data=sales_data)
When the button of each form is pressed, I want the value from the hidden input field to be passed to the quarter variable. Am I going about this all wrong? Any help would be appreciated!
Edit: When clicking the buttons to submit the forms, the values in the input tags get added to the url as the following: ?value=1
CodePudding user response:
You haved assigned value
as name
to your input
s therefore you should do
quarter = request.form.get("value")
CodePudding user response:
To fix the issue I followed Daweo's answer and
- Changed the
request.form.get("name")
torequest.form.get("value")
- Removed brackets around the values as Jarvis suggested,
- Changed the method of the form to be
POST
, notGET
- Applied this change to the python code
which fixed the issue