0

I'm struggling to discover why my CGI python script will only print one value it obtains from an order form. I have a form on order.html with the following opening tag-

<form name="cInformation" method="post" action="http://csusap.csu.edu.au/cgi-pub/bbuckl05/order.cgi" onsubmit="return validate()">

I won't post it's contents as its quite extensive, however I can guarantee that all of it's input fields have consistent names which I use in my .cgi script. My .cgi script obtains a list of values as such-

#!/usr/bin/env python

# Import modules for CGI handling 
import cgi, cgitb 

# Create instance of FieldStorage 
form = cgi.FieldStorage() 

# Order quantities, data from fields
bolt_quantity = form.getvalue('qbolt')
nut_quantity = form.getvalue('qnut')
washer_quantity = form.getvalue('qwasher')
bolt_cost = form.getvalue('total1')
nut_cost = form.getvalue('total2')
washer_cost = form.getvalue('total3')
total_cost = form.getvalue('totalcost')

And afterwards, it attempts to post them-

# Prints the customer's order information
print "<h4><p>Order Information:\n</h4>"
print "Quantity of bolts ordered: %s (%s)" % (bolt_quantity, bolt_cost)
print "<br/>Quantity of nuts ordered: %s (%s)" % (nut_quantity, nut_cost)
print "<br/>Quantity of washers ordered: %s (%s)" % (washer_quantity, washer_cost)
print "<br/><br/>Total Cost: %s" % (total_cost)

These are just portions of my code, if necessary I can provide the entire script. The problem is, that it ONLY ever correctly prints the quantity of bolts ordered, NOTHING else, as viewable in the screenshot below-

I hope I've given enough information. Can anybody help me to find and fix the issue?

Thank you,

Mohsen Safari
  • 6,669
  • 5
  • 42
  • 58
  • What does the rest of your `
    ` look like? Also, have you considered using a light web framework like [Flask](http://flask.pocoo.org/)?
    – Blender Apr 29 '13 at 21:02
  • And as for Flask, I'm completely new to web design, this is an assignment for class, so I'm not sure if I'd be permitted to use it. – itzkreator Apr 29 '13 at 21:41
  • For anyone interested, [here](http://pastebin.com/fJQiBHGW)'s a pastebin with the form – Felipe Apr 29 '13 at 21:42
  • Thank you for that ^ :) I've removed nested form tags, two elements are correctly being printed now, but no more as of yet – itzkreator Apr 29 '13 at 21:57
  • I've tested my code using a cgi script my lecturer made to handle and print any/all form elements. http://pastebin.com/VdafWapQ It works perfectly for my code (prints all elements), which is leading me to believe there's something wrong with my cgi script itself? – itzkreator Apr 29 '13 at 22:01

2 Answers2

1

The HTML you uploaded to 2shared has multiple forms in it (one per input I believe). You can't have nested forms in HTML. Most likely what is happening is that when you think you're submitting your outside form you're really submitting the inside form, in which the only input defined is 'qbolts'. You probably just need to remove all the form tags except the outermost and it should work.

See this question for more info.

PS: If you're dead-set on using cgi, you should really use form.getfirst.

Edit: You're using qwash in the form but qwasher in the script. You also don't define total___ as inputs in the form. You could use type=hidden for these guys, and make sure to update the value in the input and in the span at the same time. Or better yet, calculate the totals in python not in javascript.

Community
  • 1
  • 1
Felipe
  • 3,003
  • 2
  • 26
  • 44
  • Ooo, I see! I removed the nested forms, it's correctly printing 2 instead of 1 values now. Must be something else that I've missed. Ive definitely removed all the nested form tags now though, except for the outermost. – itzkreator Apr 29 '13 at 21:55
0

I figured it out, it was so simple and I feel so stupid. form.getvalue gets the value from the name not the id of the tag. I assigned ids to all the form data, instead of names.