Home > Blockchain >  Unable to get Value from hidden Input type using Python
Unable to get Value from hidden Input type using Python

Time:04-13

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 inputs therefore you should do

quarter = request.form.get("value")

CodePudding user response:

To fix the issue I followed Daweo's answer and

  1. Changed the request.form.get("name") to request.form.get("value")
  2. Removed brackets around the values as Jarvis suggested,
  3. Changed the method of the form to be POST, not GET
  4. Applied this change to the python code

which fixed the issue

  • Related