473,419 Members | 1,697 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

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 7858
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Glyphman | last post by:
I have a bunch of pages with long forms, with lots of input types-text, radios, textareas, and the debugging process has become overwhelming. What I need to happen is to make sure that 1. Every...
7
by: Dan | last post by:
I was trying to troubleshoot a login page that doesn't work - it keeps saying the login/password is missing - when my tracing discovered this peculiar behavior. register_globals is off, so at...
4
by: John Fereira | last post by:
So, one of the limitations of multipart-form handling is that when an <input type="file" ..> tag is used it will bring up a window which allows a user to select a file for upload but won't allow...
2
by: CSDunn | last post by:
Hello, I need some assistance with error handling in an Access 2003 Project form. The project is has a data source connection to a SQL Server 2000 database. The main form is named...
14
by: Xero | last post by:
Hello. I am using Visual Studio .NET (Academic Edition) to write a VB program. My computer is running Win XP Pro. I am writing a calculator and requires users to enter two numbers. After...
11
by: wolfesimon | last post by:
I have a form where the user copies a handwritten contract into a text field "Contract1". The contract usually exeeds 255 characters. Therefore I provided "Contract2", "Contract3", and "Contract4"...
2
by: hrpreet | last post by:
Hi All, I have few textfields and dropdowns in my jsp, form is submitted on clicking an image link. In case there is an error for instance some text field is left empty, then message is...
7
by: Dabbler | last post by:
I'm using an ObjectDataSource with a stored procedure and am getting the following error when trying to update (ExecuteNonQuery): System.Data.SqlClient.SqlException: Procedure or Function...
1
by: G04 | last post by:
Hi All, I have a continuous form with all records. For each field there is a combo in the form header and the form also contains a Toggle button "Apply Filter". When clicked, it changes to...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.