3

So I've created the following form with radio buttons, that is submitted via "POST":

<form style="margin: auto">
            <label>
              <input type="radio" name="activity" value="0"/>sedentary
            </label>
            <label>
              <input type="radio" name="activity" value="1"/>lightly active (1-3 days a week)
            </label>
            <label>
              <input type="radio" name="activity" value="2"/>moderately active (3-5 days a week)
            </label>
            <br>
             <label>
              <input type="radio" name="activity" value="3"/>heavy exercise (6-7 days a week)
            </label>
            <label>
              <input type="radio" name="activity" value="4"/>very heavy exercise (exercising twice a day or working physical job)
            </label>
          </form>

My problem is that I don't know how to access the results of this in my Python code. I'm able to access values submitted via text forms on the same page, so I don't think my POST is the issue (I'm using Flask, so request.form.get("field") does the trick there). Research shows that people have previously suggested "request.form['activity']", but this results in a 404 error; people have also suggested "request.form.get('activity','1'), but this just returns a value of '1' whether or not the radio button is even pressed.

Any help would be appreciated!

Lana Gorlinski
  • 31
  • 1
  • 1
  • 2
  • Not a Python programmer but in other apps request.form.get("activity") would return the value of the selected radio button. – Bindrid Dec 07 '16 at 17:57
  • From my understanding, only the checked radio button is submitted with the form. If no radio buttons are check, the value will be null. – Bindrid Dec 07 '16 at 18:32
  • Forgot to mention I tried request.form.get("activity") as well--did not work. – Lana Gorlinski Dec 07 '16 at 21:38
  • Did you try looking at the raw form being submitted (server side) to see what is actually there? – Bindrid Dec 07 '16 at 23:15
  • 1
    no--not sure how to do that/what that means – Lana Gorlinski Dec 08 '16 at 05:15
  • I am not sure how you would do it but in c#, you you get the keys of the query sting and list them out as show here: http://stackoverflow.com/questions/1495431/c-sharp-how-to-list-out-variable-names-and-values-posted-to-aspx-page . Since I could also do something similar when I was working on java and JSP pages, I am sure there is a way for you to list out all the keys – Bindrid Dec 08 '16 at 06:13

1 Answers1

6

In your python code, put in a submit button within the form (let's name it "submit_button").

<input type="submit" name="submit_button">

Then use this:

if request.method='POST':
   if 'submit_button' in request.form:
      user_answer=request.form['activity']
      return user_answer

This will print whatever the user chose. I have used it in my flask web application, and it works.

This should also help you: flask handle form with radio buttons

Devmonton
  • 71
  • 1
  • 3