I have a problem in web page, developed in python.
I have several fields (checkbox, textarea, etc) in form, and each field has some unique names.
I can save the value of known fields
i.e.
field_name = 'fl_textarea'
field_value = form.getvalue(field_name)
But how I can get the value of unknown field names ? I also need to save field names into the variable.
When I have printed the contents of submitted form
form = cgi.FieldStorage()
print "<p>"+ str(form) +"</p>"
It looks like this:
FieldStorage(None, None, MiniFieldStorage('flatt2695', 'abc-xyz'), MiniFieldStorage('flatt2696', 'abc-123xyz'), MiniFieldStorage('flatt2697', 'onoff'), ...
So, how I can get these field names and values one by one into the variables ?
its good you have explained everything in precise way.
Since you already have FieldStorage, so its easy for me to answer ;-)
If you want to get name of the form fields then print form.keys()
.
Other part is bit tricky. You can loop through the form.key() and write your own logic to retrieve the values on the base of keys.
I wrote a piece of compiled code here, but you can customize it according to your need.
variable = ""
value = ""
r = ""
for key in form.keys():
variable = str(key)
value = str(form.getvalue(variable))
r += "<p>"+ variable +", "+ value +"</p>\n"
fields = "<p>"+ str(r) +"</p>"
Cheers,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With