Checkboxes are not working in python web.py

276 views
Skip to first unread message

shiva krishna

unread,
Nov 15, 2012, 1:45:35 AM11/15/12
to we...@googlegroups.com
I am using web.py framework for creating small web pages.I had a basic html that has a form with four checkbox fields as below

**home.html**

    $def with ( )
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
    <title>Home</title>
    </head>
     <body>
       <form method="POST" action="/checkboxes">
          <p>check_1     <input type="checkbox" id="curly_1" value="" name="curly_1"/></p>
          <p>check_2     <input type="checkbox" id="curly_2" value="" name="curly_2"/></p>
          <p>check_3     <input type="checkbox" id="curly_3" value="" name="curly_3"/></p>
          <p>check_4     <input type="checkbox" id="curly_4" value="" name="curly_4"/></p>
          <button id="submit" name="submit">Submit</button>
       </form>
     </body>
    </html>

**index.py**

    import os
    import sys
    import web
    from web import form
    
    render = web.template.render('templates/')
    
    
    urls = (
      '/checkboxes',   'Checkboxes',  
    )
    app = web.application(urls, globals())
    
    class Checkboxes:
       
        def POST(self):
            i = web.input(groups = {})
            print i,">>>>>>>>>>>>"
            raise web.seeother('/checkboxes')
            
    if __name__ == "__main__":
        web.internalerror = web.debugerror
        app.run()  


**Result:**

    <Storage {'curly_3': u'', 'submit': u'', 'curly_4': u'', 'curly_1': u'', 'groups': [], 'curly_2': u''}> >>>>>>>>>>>>


So from the html view i can see four `checkbox` fields, i had checked all checkboxes and clicked the submit button, now it should come to `Checkboxes` class and should print the result of the input(checkboxes that are checked) in post method as i had shown above.

But in result i am getting empty string(No result) as shown above, 

can anyone please let me know whats wrong in the above code

Also how to get the values of checkboxes that are selected ?

Jason Macgowan

unread,
Nov 15, 2012, 8:27:01 AM11/15/12
to we...@googlegroups.com
There are a couple issues depending on what exactly you're trying to do.

You're getting an empty string because your checkboxes have no "value" attribute.  Also, you should probably replace your paragraph tags with label tags.  I don't know if it's a requirement, but I think checkboxes are supposed to have the same name attribute.

Here is an example:

<form method="POST">
    <input type="checkbox" id="curly_1" name="curly" value="Some Value">
    <label for="curly_1">check_1</label>
    <input type="checkbox" id="curly_2" name="curly" value="Some Other Value">
    <label for="curly_2">check_2</label>
    <button type="submit">Submit</button>
</form>

def POST(self):
    i = web.input(curly = []) #curly = [] makes sure that we don't lose values from the checkboxes.
    web.header("Content-Type", "text/html; charset=utf-8")
    return i

If you post the above form to our POST method with both boxes checked, you'll get this:
<Storage {'curly': [u'Some Value', u'Some Other Value']>

Hope this helps.



--
You received this message because you are subscribed to the Google Groups "web.py" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webpy/-/oUhrGUuPtiIJ.
To post to this group, send email to we...@googlegroups.com.
To unsubscribe from this group, send email to webpy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webpy?hl=en.

Dragan Espenschied

unread,
Nov 16, 2012, 3:42:37 AM11/16/12
to we...@googlegroups.com
> I don't know if it's a requirement, but I think checkboxes are supposed
> to have the same name attribute.

No. This is the case with radio buttons.
Reply all
Reply to author
Forward
0 new messages