We help IT Professionals succeed at work.

Python 3.4  - passing  variable values to CGI page via URL

jameskane
jameskane asked
on
I have a table of  member information as shown below. Users click on a selected name and it is necessary that the selected  memberID be sent to another page nextpage.cgi.  See from the image that the names column is url active and clicking will send a url  localhost/office_15/nextpage.cgi?memberID=121513 from this page to nextpage.cgi. This works as I can see from the pointer address over the active link

My problem is with the action page nextpage.cgi which is not processing the url correctly. I get the following error. I am attaching the nextpage.cgi code. Appreciate if someone could take a look at it.

Many thanks


Traceback (most recent call last):

  File "C:/xampp2/htdocs/OFFICE_15/NEXTPAGE.cgi", line 29, in <module>
    print(memberId[0])
NameError: name 'memberId' is not defined

Open in new window


#!C:\Python34\python.exe
import cgi,cgitb

def htmlTop():
    print("""Content-type:text/html\n\n
        <!DOCTYPE html>
        <html lang="en">
            <head>
                <meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
                <title> My Server-side template</title>
        </head>
        <body>""")

def htmlTail():
    print(""" its done</body>
        </html>""")

def getData():
    form = cgi.FieldStorage()
    memberId = form.getvalue('memberId')
    memberId = memberId[0]
    return memberId


#main program
if __name__== "__main__":
    try:
        htmlTop()
        print(memberId[0])
        
        htmlTail()
    except:
        cgi.print_exception()

Open in new window



Table with url active links
Comment
Watch Question

Mark BradyPrincipal Data Engineer
Commented:
As I see it, the memberId is returned from your function getData() but you are never calling that function. Also you can shorten the code in the getData function by changiung line  21 from
memberId = memberId[0]

to

return memberId[0]

and remove the next line.

Now in line 29 change it from

print(memberId[0])

to print getData()

Unless you need to use memberId elsewhere in your code. If so then do it like this.

memberId = getData()
print memberId

You should also use a try/accept and some checking in the getData method like so.

def getData():
    form = cgi.FieldStorage()

    try:
        memberId = form.getvalue('memberId')

        if memberId is None:
        return False;
        memberId = memberId[0]
        return memberId
    except:
        return False

Open in new window

Fixer of Problems
Most Valuable Expert 2014
Commented:
I just noticed, you have spelled memberID 3 different ways and that may be the problem.  You have 'memberID', 'memberId', and 'member_ID'.  Probably only one of those will work.

Author

Commented:
Mark and Dave,
million thanks for taking the time on my problem.  THE problem which caused me grief turns out to be the spelling - which I was careless about as I did not think CAPITALIZATION mattered !!!!!  In my sql table I named the table column memberID.  Then in the page proceeding the one in question I used memberId. Then finally in the problem page I have been using a mixture without thinking about it .

The page I put up had (by chance) consistent spelling - but in my messing about I forgot to include the GetData() function (which I had been using  in previous attempts)  

Turns out the error message is the same for both !!

I have now tidied all this up and also integrated the points you made Mark.  All is now  well !!

thanks again to you both.
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
You're welcome, glad to help.