Handling empty form fields in CGI

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Mocock

    #1

    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:



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



    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()

  • Peter Otten

    #2
    Re: Handling empty form fields in CGI

    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:
    >

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

    >
    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:



    Peter

    Comment

    • Paul Boddie

      #3
      Re: Handling empty form fields in CGI

      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

      Comment

      • Christopher Mocock

        #4
        Re: Handling empty form fields in CGI

        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.

        Comment

        Working...