For just a simple example I'd use the XHTML code:
[HTML]<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<title>formTest</title>
</head>
<body>
<form action="./cgi-bin/formTest.py" method="post">
<p>
Calculate:<br />
<input type="text" name="num1" /> + <input type="text" name="num2" /><br />
<input type="submit" value="Calculate" />
</p>
</form>
</body>
</html>[/HTML]
And the Python Code:
Expand|Select|Wrap|Line Numbers
- #!/usr/bin/env python
- import cgi
- import cgitb; cgitb.enable()
- def main():
- form = cgi.FieldStorage()
- num1 = form.getvalue("num1")
- num2 = form.getvalue("num2")
- sum = int(num1) + int(num2)
- print "content-type: text/html\n"
- print "<?xml version = \"1.0\" encoding = \"utf-8\"?>"
- print "<!DOCTYPE html PUBLIC \"-//w3c//DTD XHTML 1.1//EN\""
- print "\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n"
- print "<html>"
- print " <head>"
- print " <title>Solution</title>"
- print " <style type=\"txt/css\">"
- print " <head>"
- print " <body>"
- print " <p>Solution: " + str(sum) + "</p>"
- print " </body>"
- print "</html>"
- main()
int() argument must be a string or a number
And if I don't cast them I get an error saying:
unsupported operand types for +: 'NoneType' and 'NoneType'
Is there a way to cast the input variables from the XHTML form as something other that 'NoneType' so that I can use them in calculations?