On Wed, Feb 4, 2009 at 6:11 PM, bacoms <googlebr...@balfour.me.uk> wrote:
> Mike, First of all many thanks for the detailed reply. I thought I
> understood what you were saying but, having changed my code
> accordingly, I'm not so sure.

You are welcome.  It looks like you understood things pretty well.
>
> If I understand you right, to embed a graph on my HTML page I need to
> create it outside the HTML with Content-type: image/png\n\n. This is
> achieved, at the point in my HTML where the graph is to be displayed,
> by the inclusion of  an IMG tag.
> (E.G. <img src='SBFPAchart.cgi?start-date=01/01/2008&end-
> date=09/01/2008&1=on&2=on'/>)

Try: Content-Type instead of Content-type
I missed this yesterday, sorry.

> Presumably what is happening here is that the HTML with the IMG tag is
> generated on the server and sent to the browser on the client. The
> browser see the embedded IMG tag and requests the server to run the
> script specified on it. This the server does, the graph image is
> created on the server and sent to the browser which now adds it to the
> location on the page indicated by the HTML.

Yes, this is correct.

> On this premise I:
>
> a) removed the lines relating to the graph generation from my script
> responsible for generating the HTML and replaced them with the IMG tag
> above.
> b) wrote script SBFPAchart.cgi to process the parameters passed back
> from the browser, call my database manager to generate the data
> arrays, and use GD::Graph to create the image using this data. This
> image is then output, preceded with the Content-type: image/png\n\n
> statement.

Try Content-Type instead of Content-type the case may matter.
I realize I used a lower case t yesterday, sorry.  Pretty sure this is
platform dependent, both the OS and web server may or may not care,
but its a quick and easy change.  I know that apache on linux returns
Content-Type to the client.

> The net result is that SBFPAchart.cgi script runs (the trace file
> proves this) but no image is displayed (just the red cross in a box
> placeholder). (If I uncomment the line that pushes the image onto the
> trace array, I see what I assume to be the image in the trace file but
> it's not in binary so I can't cut and paste it to a file to view.)
If you comment out everything that writes to the file /except/ for the
print $image->png line you should wind up with a valid png file.

> I tried to understand what you meant by-
>>
>> You can either do this by commenting out the content-type head and
>> redirecting the output.
>> ./test.cgi >test.png

> but couldn't figure out how to code this in the context of my code. I
> tried coding in my HTML
>              <img src='SBFPAchart.cgi?start-date=19/01/2009&end-
> date=23/01/2009&58=on&242=on>test.png'/> and
>             <img src='SBFPAchart.cgi>test.png?start-
> date=19/01/2009&end-date=23/01/2009&58=on&242=on'/>
> but couldn't find file test.png anywhere.

You cannot do the redirect within the html tag, that will not work.
For one thing the browser would probably thing the > char was part of
a tag.

What I meant was to run the script at the command line.  Looks like
you are running windows, so open a dos box and move to the directory
where your cgi program lives then run the script from there.

like: SBFPAchart.cgi >test.png
Haven't used perl for cgi stuff on windows in a while, you may need to use
d:\perl\bin\perl SBFPAchart.cgi >test.png
if perl isn't in your path
You need to do the redirection from the command line, not within the html.
For one thing the browser would probably interpret the > char as the
end of the image tag.  Secondly, the point of doing this from the
command line on the server is to eliminate any problems due to the web
server, or its interaction with the browser and simply test to see if
the code generates an image when run all by itself.

A couple other things jump out here:

Before you do anything else look at the text file of debug info and
see what the dates look like.

You have slashes in the arguments to the script, that violates the
http protocol rules.
There are a number of ways around this problem.

Replace slashes with %2f when calling the script:

Instead of:
SBFPAchart.cgi?start-date=19/01/2009&end-date=23/01/2009

something like: "19%2f01%2f2009" in the html
SBFPAchart.cgi?start-date=19%2f01%2f2009&end-date=23%2f01%2f2009

and then have the script fix them up, replacing the %2f sequences with / chars:

$startDate =~ s/%2f/\//g;

Do this before you do this:

>    my($startDD, $startMM, $startYYYY) = ($startDate =~ m^(\d\d)/(\d
> \d)/(\d\d\d\d)^);

There is a function in the CGI.pm module called escapeHTML that will
return a string
The URI::Escape module can handle this for you as well.

It may be easier to just use some other char as a separator.

As a test, you could try hard-coding the values of the script
parameters and removing the parameters from the html where you call
the script.

> Thanks to your help I feel I'm almost there. If you could give me a
> few pointers on what to try next, I'd really appreciate it.

Yes it looks like you are really close.

Good luck, let us know how it works out.

Mike

-- 
To unsubscribe, e-mail: beginners-cgi-unsubscr...@perl.org
For additional commands, e-mail: beginners-cgi-h...@perl.org
http://learn.perl.org/


Reply via email to