1
votes

I am trying to process HTML form input. I have a CGI file that I want to collect all the data from, including the check boxes and radio buttons. I am trying to use cgi.FieldStorage but something is not working.

Here is an example of what I am trying to do:

form = cgi.FieldStorage()
name = form.getvalue('sensitivity')
print name

But this is returning none. Here is a snippet of the CGI file:

if config_settings.settings[5] == '1':

    print'''<html><label class="checkbox inline control-label"><input name="aWeight" value="1" type="checkbox" checked/></html>'''
else:
    print'''<html><label class="checkbox inline control-label"><input name="aWeight" value="1" type="checkbox"/></html>'''
print'''<html><span> A-Weight &nbsp;&nbsp;&nbsp;</span></label></html>'''

This sets a checkbox depending on the content of an XML tag in another file being set to 1 or 0. The XML file and the Python file are working together fine. What I am trying to acheive is to collect the data from the checkboxes when a user changes them.

I have the this code at the beginning of my CGI script:

<form class="well form-inline" method="post" action="/cgi-bin/process_setup.py">

And I though that this would allow me to process/collect the from data with cgi.FieldStorage but it does not seem to be working. Any advice?

1
Please check that you import following in python cgi: import cgi, cgitb - Snehal Parmar
@SnehalParmar Hi, Yes I have done that but it is still not working for me. - Mark Kelly

1 Answers

0
votes

I think you are missing this print ("Content-type:text/html\r\n\r\n") for python 3 and this print "Content-type:text/html\r\n\r\n" for python 2.

So you want to prevent the user to change the checkbox value? If yes than here is a way :

<label id='checky'><input type="checkbox" name="checky" onchange="changeCheck(this)" checked="" ></label>
<script type="text/javascript">
function changeCheck (element) {
     element.checked = !element.checked;
}
</script>