laila
laila

Reputation: 1059

Saving results of radio button click in Flask

The relevant part of my flask code is:

@app.route("/process", methods = ["GET", "POST"] )
def process_form():
    #checked = request.form.getlist('option')
    checked=request.form('option')
    with open('checked.txt','w') as file:
        file.write("%s"%checked)
    # do something with checked array
    return checked

and my html looks like:

<div class="container" id='tog'>
     <div class="half">
        <form action="/process" method="POST">
        <input type="radio" name="option" value="original" />
        <h3>originak value<h3>
    </div>

    <div class="half"> 
        <input type="radio" name="option" value="freq" />
        <h3>Term Freq<h3>
    </div>
    </form>
    </div>

The idea is that I want to find out which radio button was selected and store the information is a text file. But that does not seem to be working.

I changed the html to:

<form action="/process" method="POST">    
    <div class="half">
        <input type="radio" name="option" value="original" />           
    </div>
    <div class="half">
        <input type="radio" name="option" value="freq" />           
    </div>
        <input type="submit" value="Submit">

</form>

and now I get a 400 Bad Request error

Upvotes: 0

Views: 10311

Answers (1)

Bidhan
Bidhan

Reputation: 10697

You don't have a submit button inside of your form. Add one.

<input type="submit" value="Submit!" />

Also, you probably want to change request.form('option') to request.form['option']

Upvotes: 2

Related Questions