i have an html form with a drop-down list that lists a bunch of images.
how can i get my cgi script to load the image ?
i have imported cgi and os
text entered from a form and read from a file do ok, but i cant seem to get it to load an image.

the form writes all of the entries as lines in a .txt file
this file is selected later,
all the lines are read,
and based on the first line of the text file determines the name of the .gif image to load.

is there a way to do this, i think i am getting all the ' and '' stuff confused.

thanks.

Interesting project, but I need a little more info. What does your code look like and your text file?

ok, here ya go

#!/usr/bin/python
import os
import cgi
import cgitb; cgitb.enable()
from time import gmtime, strftime

the_time = strftime("%A, %B %d, %Y") #Get a TimeStamp

#This script takes data from ViewRecords.py and opens and views the 
#Selected text file
#some bug some where

form = cgi.FieldStorage()
DataRecord = form['DataTime'].value
Customer = form['CustName'].value 

# Set up the html stuff
reshtml = """Content-Type: text/html\n
<html>
 <head><title>Customer Data</title></head>
<body>
"""


print reshtml
print '<big><span style="font-weight: bold;">Watkins Crop Consulting</span>&nbsp;'
print '</big>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
print '&nbsp;&nbsp;<big>'+the_time+'</big>'
print '<br>'
print "1915 Cherokee <br>"
print "Dalhart, Tx 79022 <br>   333-5943&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<H2>"+Customer+"</H2>"


fileHandle = open("/var/www/stretch/web_root/SidCrops/"+Customer+"/"+DataRecord, "r")
lineList = fileHandle.readlines()
fileHandle.close()
Line_Iters = len(lineList)/7 #number of lines in the file divided by 7 (how many fields there are)


Iterations = 0 #represents the number of times the following while loop will run
NextStep = 0 #Increments to the next line of the file 
while Iterations < Line_Iters:
	print '<meta'
 	print 'content="text/html; charset=ISO-8859-1" http-equiv="content-type">'
  	print '<title></title>'
	print '</head>'
	print '<body>'
	print '<table style="text-align: left; width: 100%;"'
 	print 'border="0" cellpadding="0" cellspacing="0">'
  	print '<tbody>'
   	print '<tr>'
     	print '<td><img'
 	print 'style="width: 150px; height: 150px;" alt="circle"'
	print 'src="http://stretchweb.org/"'+lineList[NextStep]+".gif"+"></td>'
      	print '<td>'
	NextStep = NextStep + 1
	print "<span style='font-weight: bold;'>Field -</span>&nbsp;&nbsp;&nbsp;&nbsp; "+lineList[NextStep]+"	"
	NextStep = NextStep + 1
	print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
	print "<span style='font-weight: bold;'>Crop -</span>&nbsp;&nbsp;&nbsp;&nbsp; "+lineList[NextStep]+"	"
	NextStep = NextStep + 1
	print "<span style='font-weight: bold;'>GS -</span>&nbsp;&nbsp;&nbsp;&nbsp; "+lineList[NextStep]+"	"
	NextStep = NextStep + 1
	print '<br>'
	print '<br>'
	print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
	print "<span style='font-weight: bold;'>Water -</span>&nbsp;&nbsp;&nbsp;&nbsp; "+lineList[NextStep]+"	"
	print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
	print "<br>"
	print "<br>"
	NextStep = NextStep + 1
	print "<span style='font-weight: bold;'>Remarks -</span>&nbsp;&nbsp;&nbsp;&nbsp; "+lineList[NextStep]+"	"
	print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
	NextStep = NextStep + 1
	print "<br>"
	print "<br>"
	print "<span style='font-weight: bold;'>Treatments -</span>&nbsp;&nbsp; "+lineList[NextStep]+"	"
	print "<br>"
	NextStep = NextStep + 1
	Iterations = Iterations + 1
	print "<br>"
	print "<br>"
	print '</td>'
    	print '</tr>'
  	print '</tbody>'
	print '</table>'
	print '<br>'
	print '<br>'
	print '<br>'
	print '<br>'

	
print "<br>"
print  "</body>"
print "</html>"

and a sample of the text file lines

circle1.gif
line
of
text
will
always
be a factor of seven
circle2.giff
because
the .py file
that writes
the text from the form
writes seven lines at
a time

hope this helps...

by the way, if i dont use a variable but just print image="circle.giff"
it all works fine

thanks

nevermind, i found a work-around

Awsome, care to share for the next person who comes by looking for the same help? :)

um.. sure.
instead of having the script write just the name of the image file to load , i had it write the whole html line when the image is selected like this

image = form['image'].value

then later....

OutFile.write('src="http://stretchweb.org/'+image+'"></td>\n')

the script that reads the lines, just does this...

print image...

and it worked !

if you need more details or want to see the whole script, let me know

Gee nephish,
I am still scratching my head trying to figure out why the first version did not work and you come up with a working alternate. Great job!!!

By the way, you are right this line is a ""/' nightmare, my editor flags it down as an error, wants a " at the end ...

print 'src="http://stretchweb.org/"'+lineList[NextStep]+".gif"+"></td>'

Maybe you can triple quote the whole spiel.

Also, the indents in the while loop are not consistent, are you mixing up tabs and spaces? I always use spaces since tabs are not set the same with every computer.

Are you using .gif or .giff?

oh, i am using a gif.
and it works now, but here is what i had to do.
i have the script that writes the line to the text file write

print 'src="http://stretchweb.org/"'+lineList[NextStep]+".gif"+">

and then the script that prints the image just reads this line from a text file
and prints it

print line[1]

in the middle of it printing other html stuff.

gee whiz, it works, but ...... man......

thanks for your help.
not a very clean way to do it.. but it works right now.
i think very much that the problem i was having was in the line up
of quotes...because the error i kept getting was about not ending script headers...

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.