Neemaximo
Neemaximo

Reputation: 20771

CGI form submit button using python

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')

Upvotes: 3

Views: 8025

Answers (1)

timc
timc

Reputation: 2174

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>'

Upvotes: 5

Related Questions