473,765 Members | 2,021 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.FieldStorag e()

test()

Jan 26 '07 #1
3 7888
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("m yfield") 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.FieldStorag e()
Try replacing this with...

form = cgi.FieldStorag e(keep_blank_va lues=1)

Paul

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

form = cgi.FieldStorag e(keep_blank_va lues=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
2102
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 field on the page is filled out-if not, a list of incorrect fields is displayed, and any radio boxes that were checked still are 2. The fields are queried to a table in a database. (The table has the same column names as the field names) Here's...
7
11879
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 the top of my script I assign a few variables to incoming GET and POST values. $login = clean($_POST, 30); $passwd = clean($_POST, 30);
4
2822
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 the user to select multiple files. As you may know, the tag produces a text input field with an adjacent button for selecting a file from the local file system. As I would like to be able to allow the user to upload an arbitrary number of files...
2
1997
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 ‘frmSelectByTest', and its subform's ‘Name' and ‘Source Object' properties are ‘frmSelectByTestSub'. The ‘Default View' on the main form is ‘Single Form', and on the sub form is ‘Continuous Forms'. The users select the name of a test (uniquely identified by a...
14
1599
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 entering the numbers, they should click a button called 'Display Results'. How can I display a dialog box prompting the users to check their entry if they left any of the boxes empty? I have tried the .IsNaN function but in vein. Thanks.
11
4155
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" fields. When "Contract1" cannot accept further input, the user continues on to "Contract2", ect. I don't want to use memo fields for obvious reasons. Does anyone have an event procedure and code, which could monitor
2
1825
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 displayed below respective fields ( using spans, inner html). The message is displayed onblur event of the field. So if there was an error then message is displayed, now if the user corrects the error and clicks the link then first the message...
7
3469
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 'UpdateRegistrant' expects parameter '@EMail', which was not supplied. The field value was null in the database and not changed in the FormView so is null going back into the stored procedure. I'm stumped and would greatly appreciate any suggestions.
1
6213
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 "Cancel Filter". Then an SQL string with condition meeting the criteria input in the combo boxes is built. I have noticed that when certain fields are not filled in (no space, nothing), the record is simply not shown. The empty field is not necessarily...
0
9404
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7379
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5277
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.