1

I am new to flask (and StackOverflow) and I apologise in advance for any mistakes I have made.

I am trying to get selected value from a select tag in flask. I have tried all the solutions of similar questions on stack-overflow, but none of them seem to be working for me. I am confused as to, do I need a button to submit my selected value or does it automatically gets sent when an option is selected.

What I want to do is get value from select tag in html and on the basis of that display some text in a readonly text-area. Flask is not showing any error but at the same time I cannot see the value as well. Thanks a lot!

Python Code

@app.route('/learn', methods=['GET','POST'])
def clearn():
    if request.method == 'POST':
        cat = request.form['category']
        print(cat)
    return render_template('index.html', clearn = cat)

HTML

<form action="/learn" method="POST" class="select_cat" id="select_cat">
    <div class="image fit">
        <div class="12u$">
            <div class="select-wrapper">
                <select name="category" id="category" form="select_cat">
                    <option value="0">- Select Procedure -</option>
                    <option value="opt1">Option 1</option>
                    <option value="opt2">Option 2</option>
                    <option value="opt3">Option 3</option>
                    <option value="opt4">Option 4</option>
                </select>
            </div>
        </div>
        <div class="12u$">
            <textarea class="clearn" name="message" id="message" placeholder="Procedure" rows="6" readonly>{{clearn}}</textarea>
        </div>
    </div>
</form>

1 Answer 1

0
PY file

@app.route('/learn', methods=['GET','POST'])
@app.route('/learn/<category>/') //in here you can define the datatype if needed ie. @app.route('/learn/<int:category>/') 

def clearn(category):
    if request.method == 'POST':
    return render_template("index.html", category= category)

HTML file

<div class="12u$">
    <div class="select-wrapper">
        {{category}}
    </div>
</div>
Sign up to request clarification or add additional context in comments.

3 Comments

While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply
Hey, thanks a lot for the reply, I am afraid this is not the question I has asked. What I want to do is get value from select tag in html and on the basis of that display some text in a readonly text-area. Also please do correct me if I am wrong, and sorry for not being specific in the question.
i would use ajax + jquery. If that works for you i can elaborate more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.