-1

I'm looking to pass the selected value of an HTML dropdown to my Python script as per below:

main.py

@app.route('/select', methods=['POST', 'GET'])
def select():
    operator = request.form.get('operator')

index.html

<select class="form-control" id="operator" name="operator">
        <option value="=">=</option>
        <option value=">">></option>
        <option value="<"><</option>
    </select>

The HTML dropdown works fine as far as I can tell, but when I run main.py, nothing is assigned to operator

alangap23
  • 1
  • 1

1 Answers1

0

It's not clear from your code where you're having trouble.
The following example shows you how to send a form with a SelectBox to the server and receive the values there.

@app.route('/select', methods=['GET', 'POST'])
def select():
    if request.method == 'POST':
        op = request.form.get('operator')
        print(op)
    return render_template('select.html')
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <form method="post">
      <select name="operator">
        <option value="=">=</option>
        <option value=">">></option>
        <option value="<"><</option>
      </select>
      <input type="submit" />
    </form>
  </body>
</html>
Detlef
  • 6,137
  • 2
  • 6
  • 24