Search code examples
pythoncheckboxcherrypy

checkbox values in cherrypy


I understand that Cherrypy makes checkbox values available as a list cfg question (CherryPy - saving checkboxes selection to variables)

Let's say I have following form data:

... snip ...
<input type=checkbox id="1">
<input type=checkbox id="1">
<input type=checkbox id="1">

<input type=checkbox id="2">
<input type=checkbox id="2">
<input type=checkbox id="2">

<input type=checkbox id="3">
<input type=checkbox id="3">
<input type=checkbox id="3">
... snip ...

Then Cherrypy makes this available as:

{'1': [u'on', u'on', u'on'],'2': [u'on', u'on', u'on'],'3': [u'on', u'on', u'on']}

From the moment I uncheck the second checkbox id3 then I get:

{'1': [u'on', u'on', u'on'],'2': [u'on', u'on', u'on'],'3': [u'on', u'on']}

With this I'm unable to say which checkbox is unchecked, .... I could when 'off' would be used when a checkbox is unchecked.. but that's not the case.

Any ideas how to tackle this?

Cheers,

Jay


Solution

  • First a nit: The "id" attribute in HTML is supposed to be unique for the whole document.

    You then have two options:

    1. Change the "name" attributes to be unique, like <input type="checkbox" name="3b">, in which case you'll get back {..., '3a': u'on' '3c': u'on'}, or
    2. Make the values unique, like <input type="checkbox" name="3" value="b">, in which case you'll get back {..., '3': [u'a', u'c']}.