hi all,

i need to get all the data in the list (which is posted in the form) and store it, separating each list item with comma's. Its really clear in the code...this is what ive tried so far..it doesnt compile and it seems to complain about this line of code photos = form["photolist"]...thanks for any help

html form--->add.html

<form name="listadd" method=post action=addDo.cgi>
     <select name="photolist" size="9" style="width: 500; height: 50"></select>
     <input type="submit">
</form>

python cgi script--->addDo.cgi

import cgitb; cgitb.enable()
import cgi
import sys

form = cgi.FieldStorage()
photos = form["photolist"]

i=0
for photo in photos:
    i = i + 1
    finalPhoto = finalPhoto + "," + photo[i]

All I could find in my Python toolbox that pertains to the retrieval of form values is this little NET module written by Mark Lee Smith. It is supposed to make CGI programming in Python easier.

Here is a small module/httpwrapper that I wrote that parses GET, POST and SERVER data and stores seperate lists of keys, values and a dict of pairs of each.

# Alexander Sherwin
# 08/14/2005 v1.2
#
# This class is designed to look for and parse miscellaneous data
# that is not handled internally through Python (i.e. POST, GET.)
#
# Currently, upon instantiation of the class it automatically parses
# all POST, GET and SERVER data available to the script.
#
# Report any errors, bugs or contributions & additions
# to alex.sherwin@gmail.com.  This class has been developed and tested on 
# nearlyfreespeech.net servers (FreeBSD, Apache 2.x.)

import sys
import os

class HTTPWrap:
	"""Takes the sys.stdin sent from the webserver and parses it for POST variables
	This class may not work if your webserver sends other types of information to 
	serverside scripts as sys.stdin

	.process_post()	- Initiates the parsing of raw data in std.in
	.process_get()	- Initiates the parsing of ENV var QUERY_STRING
	.server(str)	- Checks SERVER vars in server_dict and returns the value
			- if the key exists, False if it doesn't

	.post_keys	- List of POST keys (form names)
	.post_vals	- List of POST values (form values)
	.post_dict	- Dict of POST keys and values
	.get_keys	- List of GET keys
	.get_vals	- List of GET values
	.get_dict	- Dict of GET keys and values
	.server_keys	- List of SERVER keys
	.server_vals	- List of SERVER values
	.server_dict	- Dict of SERVER keys and values

	.has_post	- True if script has received POST data
	.has_get	- True if script has received GET data

	.server_dict	- Dict of SERVER keys and values"""

	post_keys = []	# init all lists & dictionaries
	post_vals = []
	post_dict = {}
	get_keys = []
	get_vals = []
	get_dict = {}
	server_keys = []
	server_vals = []
	server_dict = {}

	has_post = False	# Instantiates with no POST vars
	has_get = False		# Instantiates with no GET vars

	def __init__(self):
		"""Initialize by determining if there is any POST or GET vars"""

		self.__raw = sys.stdin.readline()	# read from sys.stdin
		if len(self.__raw):
			self.process_post()	# if POST string length > 0, parse it

		self.__raw = ""				# init empty string

		for k in os.environ.items():
			if "QUERY_STRING" in k:			# find GET data
				self.__raw = k[1]		# save GET data
			else:
				self.server_dict[k[0]] = k[1]	# save SERVER vars
				self.server_keys.append(k[0])
				self.server_vals.append(k[1])

		if len(self.__raw):
			self.process_get()	# if GET string length > 0, parse it

	def server(self, serverVar):
		"""Check server_dict for a server variable, return it's value"""

		if self.server_dict.has_key(serverVar):
			return self.server_dict[serverVar]	# return value
		else: return False

	def process_post(self):
		"""Read sys.stdin and parse it to obtain orderly POST data"""

		self.has_post = True		# set boolean for exterior uses
		rawlist = self.__raw.split('&')	# obtain list of key=val strings

		for k in rawlist:
			tmpli = k.split('=')			# get key, val list
			self.post_keys.append(tmpli[0])		# save key
			self.post_vals.append(tmpli[1])		# save value
			self.post_dict[tmpli[0]] = tmpli[1]	# save pair in dict

	def process_get(self):
		"""Parse self.__raw to obtain orderly GET data"""

		self.has_get = True		# set boolean for exterior uses
		rawlist = self.__raw.split('&')	# obtain list of key=val strings

		for k in rawlist:
			tmpli = k.split('=')			# get key, val list
			self.get_keys.append(tmpli[0])		# save key
			self.get_vals.append(tmpli[1])		# save value
			self.get_dict[tmpli[0]] = tmpli[1]	# save pair in dict

I am about as new as you can get to form retrieval and pass the variables to my python code. I'm sure that these scripts can help but is there an example that I can look at where these scripts are implemented.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.