CGI form submit button using python

杀马特。学长 韩版系。学妹 提交于 2019-12-06 07:21:41

问题


I am trying to create a cgi form that will allow a user to type in a word and then it will take that word and send it off to the next page (another cgi). I know how to do it with a .html file but I am lost when it comes to doing it with python/cgi.

Here is what I need to do but it is in html.

<html>
<h1>Please enter a keyword of your choice</h1>
<form action="next.cgi" method="get">
Keyword: <input type="text" keyword="keyword">  <br />
<input type="submit" value="Submit" />
</form>
</html>

Does anyone know how to create a submit button with cgi? Here is what I have so far.

import cgi
import cgitb
cgitb.enable()


form = cgi.FieldStorage()

keyword = form.getvalue('keyword')

回答1:


To display html from a Python cgi page you need to use the print statement.

Here is an example using your code.

#!/home/python
import cgi
import cgitb
cgitb.enable()

print 'Content-type: text/html\r\n\r'
print '<html>'
print '<h1>Please enter a keyword of your choice</h1>'
print '<form action="next.cgi" method="get">'
print 'Keyword: <input type="text" name="keyword">  <br />'
print '<input type="submit" value="Submit" />'
print '</form>'
print '</html>'

Then on your next.cgi page you can get the values submitted by the form. Something like:

#!/home/python
import cgi
import cgitb
cgitb.enable()

form = cgi.FieldStorage()

keyword = form.getvalue('keyword')

print 'Content-type: text/html\r\n\r'
print '<html>'
print keyword
print '</html>'


来源:https://stackoverflow.com/questions/13814241/cgi-form-submit-button-using-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!