简体   繁体   English

Python进程HTML / CGI表单输入

[英]Python process HTML / CGI form input

I am trying to process HTML form input. 我正在尝试处理HTML表单输入。 I have a CGI file that I want to collect all the data from, including the check boxes and radio buttons. 我有一个CGI文件,我想从中收集所有数据,包括复选框和单选按钮。 I am trying to use cgi.FieldStorage but something is not working. 我正在尝试使用cgi.FieldStorage,但有些东西不起作用。

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: 这是CGI文件的片段:

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. 这将设置一个复选框,具体取决于另一个文件中XML标记的内容设置为1或0. XML文件和Python文件正在协同工作。 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: 我在CGI脚本的开头有这个代码:

<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. 而且我认为这可以让我使用cgi.FieldStorage处理/收集数据,但它似乎不起作用。 Any advice? 有什么建议?

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. 我认为你缺少这个用于python 3的print ("Content-type:text/html\\r\\n\\r\\n")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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM