I'm having problems casting input information from an XHTML form as a different type in a CGI script using Python.
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="Calculat e" />
</p>
</form>
</body>
</html>[/HTML]
And the Python Code:
However when I try to cast num1 and num2 as integers, I get an error saying that only string or number arguments can be used:
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?
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="Calculat e" />
</p>
</form>
</body>
</html>[/HTML]
And the Python Code:
Code:
#!/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?
Comment