Handle Multiple Checkboxes with a Single Serverside Variable

Question:

I have the following HTML code:

<form method="post">
              <h5>Sports you play:</h5>
                <input type="checkbox" name="sports_played" value="basketball"> basketball<br>
                <input type="checkbox" name="sports_played" value="football"> football<br>
                <input type="checkbox" name="sports_played" value="baseball"> baseball<br>
                <input type="checkbox" name="sports_played" value="soccer"> tennis<br>
                <input type="checkbox" name="sports_played" value="mma"> MMA<br>
                <input type="checkbox" name="sports_played" value="hockey"> hockey<br>

                <br> 

                    <input class="btn" type="submit">

</form>

And then ideally I would like to have the following python serverside code:

class MyHandler(ParentHandler):
    def post(self):
        sports_played = self.request.get('sports_played')
        #sports_played is a list or array of all the selected checkboxes that I can iterate through

I tried doing this by making the HTML sports_played name and array, sports_played[], but that didn’t do anything and right now it just always returns the first selected item.

Is this possible? Really I just don’t want to have to do a self.request.get(‘HTML_item’) for each and every checkbox incase I need to alter the HTML I don’t want to have to change the python.

Thanks!

Asked By: clifgray

||

Answers:

The answer is shown in the webapp2 docs for the request object:

self.request.get('sports_played', allow_multiple=True)

Alternatively you can use

self.request.POST.getall('sports_played')
Answered By: Daniel Roseman

The name of the inputs should have [] at the end so that they are set to the server as an array. Right now, your multiple checkboxes are being sent to the server as many variables with the same name, so only one is recognized. It should look like this:

<form method="post">
              <h5>Sports you play:</h5>
                <input type="checkbox" name="sports_played[]" value="basketball"> basketball<br>
                <input type="checkbox" name="sports_played[]" value="football"> football<br>
                <input type="checkbox" name="sports_played[]" value="baseball"> baseball<br>
                <input type="checkbox" name="sports_played[]" value="soccer"> tennis<br>
                <input type="checkbox" name="sports_played[]" value="mma"> MMA<br>
                <input type="checkbox" name="sports_played[]" value="hockey"> hockey<br>

                <br> 

                    <input class="btn" type="submit">

</form>

Now, if you select more than one, the values will be sent as an array.

Answered By: Manny Fleurmond

Although this answer is not related to this question, but it may help all the django developers who is walking here and there.

In Django request.POST is a QueryDict object. So you can get all the values as list by following way

request.POST.getlist('sports_played')

N.B: This only works in Django

Answered By: Emdadul Sawon

This is not the answer to the original question, but Flask developers might find it helpful.

In Flask you can use request.getlist() method:

request.getlist('sports_played')
Answered By: Denis Petukhov
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.