469,898 Members | 1,758 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

Handling empty form fields in CGI

Hi all,

Bit of a python newbie so need a little help with a CGI script I'm
trying to write. I've got it working fine as long as the fields of the
form are filled in correctly, however I need to be able to accept blank
entries. Therefore I want to convert any empty entries to an empty string.

For example, if I call the following CGI script as:

http://localhost/cgi-bin/test.cgi?myfield=hello

....it prints hello in the browser. But if I call it as:

http://localhost/cgi-bin/test.cgi?myfield=

I get an exception. I want it to treat myfield as an empty string and
not throw an exception.

Any suggestions?

Thanks in advance, Chris.

Script below:
#!/usr/bin/env python2
# Import CGI and CGI debug libraries
import cgi
import cgitb; cgitb.enable()

def test():
value=form["myfield"].value
print value

# Start of main code
print 'Content-type: text/html'
print

# Get the contents of the query string
form = cgi.FieldStorage()

test()

Jan 26 '07 #1
3 7375
Christopher Mocock wrote:
Bit of a python newbie so need a little help with a CGI script I'm
trying to write. I've got it working fine as long as the fields of the
form are filled in correctly, however I need to be able to accept blank
entries. Therefore I want to convert any empty entries to an empty string.

For example, if I call the following CGI script as:

http://localhost/cgi-bin/test.cgi?myfield=hello

...it prints hello in the browser. But if I call it as:

http://localhost/cgi-bin/test.cgi?myfield=

I get an exception. I want it to treat myfield as an empty string and
not throw an exception.

Any suggestions?
You can catch the exception:

def test():
try:
value = form["myfield"].value
except KeyError:
value = ""
print value

or use form.getlist("myfield") and then check the length of the returned
list of values.

With both options you also have to decide what to do when 'myfield' is given
twice:

http://localhost/cgi-bin/test.cgi?myfield=42&myfield=24

Peter
Jan 26 '07 #2
Christopher Mocock wrote:
>
Bit of a python newbie so need a little help with a CGI script I'm
trying to write. I've got it working fine as long as the fields of the
form are filled in correctly, however I need to be able to accept blank
entries. Therefore I want to convert any empty entries to an empty string.
[...]
form = cgi.FieldStorage()
Try replacing this with...

form = cgi.FieldStorage(keep_blank_values=1)

Paul

Jan 26 '07 #3
Paul Boddie wrote:
Try replacing this with...

form = cgi.FieldStorage(keep_blank_values=1)

Paul
That worked a treat. Thanks very much to both who replied.

Chris.
Jan 29 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Glyphman | last post: by
7 posts views Thread by Dan | last post: by
4 posts views Thread by John Fereira | last post: by
14 posts views Thread by Xero | last post: by
2 posts views Thread by hrpreet | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.