views:

79

answers:

1

I am messing around with a tornado web app with which I need a bit of help. I have multiple checkboxes with the same name and I would like to POST the values of the selected one.

<input id=DB_BASED_ID name="activity" value=DB_BASED_ID type="checkbox"/>
<input id=DB_BASED_ID name="activity" value=DB_BASED_ID type="checkbox"/>
<input id=DB_BASED_ID name="activity" value=DB_BASED_ID type="checkbox"/>
<input id=DB_BASED_ID name="activity" value=DB_BASED_ID type="checkbox"/>

I can get the values of each with javascript pre-POST but am having troubles getting this list on the python (tornado) side. i only get the highest selected value.

on the python side it looks like:

...

def post(self):
    email = self.get_argument("email")
    activity = self.get_argument("activity")
+1  A: 

It's fine to let multiple tags have the same name attribute, but the id attributes must be unique -- here, they're not (unless each of those occurrences of the identical DB_BASED_ID is somehow meant to be replaced with a different value? But then why not show the things actually distinct, as they do appear in the real HTML?!), making this invalid HTML and subject to all sorts of problems.

Once this problem is fixed, in those handler methods, self.request.arguments['activity'] (if that string key is present in said directory) will be a list of non-empty values for all inputs named 'activity' (if any).

Alex Martelli
thank you so much. each id is different. my goal is to get a list of activities that someone in which a person is interested. i was hoping to pass an array of "checked" items back to the database. so the actual html would look more like <input id="1" name="activity" value="1" type="checkbox"/>Hiking<input id="2" name="activity" value="2" type="checkbox"/>Biking<input id="3" name="activity" value="3" type="checkbox"/>Camping<input id="4" name="activity" value="4" type="checkbox"/>Fishingi only care about the numeric value since that corresponds to the pk of the corresponding activity
swasheck
@swash, then the code I give in the 2nd pararaph will work, except that you need to do your own filtering and conversion to integers, since the results will always be strings.
Alex Martelli