chrisharris has asked for the wisdom of the Perl Monks concerning the following question:

I have a perl script that allow people to fill in an HTML form, then the script returns the same page but with all the values filled in so they can print.
Unfortunately, when you try to print the perl script's output, Netscape will only give the message that:
DATA MISSING
This document resulted from a POST operation and has expired from the cache. If you wish you can repost the form data to recreate the document by pressing the reload button.

It works perfectly in the browser, you can even hit reload and it does what it should.

My guess is there's some sort of variable to set the cache correctly?
Any help will be very appreciated, thanks...
-chis

Replies are listed 'Best First'.
Re: Data Missing?
by chromatic (Archbishop) on Apr 11, 2000 at 18:05 UTC
    There are two other options you might take in a similar situation:
    • Return the values as plain text, not in the form widgets (text boxes, text fields, radio buttons). Almost as easy as returning the values in the widgets.
    • Use PerlMagick or a PostScript module to turn the input into a PDF or a PS file, and return that to the browser for printing.
    I'd go with the first option there, of these two, but be aware that you can do the second. I've seen actual invoices returned to the browser from particularly adept programmers. ("Programming Web Graphics with Perl and GNU Software" has a lot of good examples.)
Re: Data Missing?
by btrott (Parson) on Apr 11, 2000 at 09:58 UTC
    Typically browsers don't cache CGI output. You can adjust this by messing with the cache headers sent back from the web server. Are you using CGI.pm? Just use the -expires value in the header method:
    print $query->header(-expires=>'+3d');
    That'll set the document to expire in three days. You may want something different. :) Look at the docs for CGI.pm.

    You could also look at the HTTP spec for the relevant headers. There's a Cache-Control header, specifically.

    Finally, you could also try changing to a GET method if it's not too much trouble.

Re: Data Missing?
by comatose (Monk) on Apr 11, 2000 at 17:32 UTC

    Another potential solution is to use the GET method instead of POST. However, GET has a 256 character limit on some servers which means this might not be the solution for you if the results of your form are longer than that.

    If you are using CGI.pm, you shouldn't have to change the code for generating the print page at all. You would just need to change the method of your form.

    One thing to be aware of though is that because a GET CGI query is all in the location the following happen:

    • All the data they entered goes into your log
    • A caching server somewhere in the client's network may cache the results for the next person from that network
    • Because all the data goes with the request URL, administrators catching URLs (or any sniffer for that matter) on the network can see what someone entered.

    Just some things to consider.

Re: Data Missing?
by httptech (Chaplain) on Apr 11, 2000 at 17:44 UTC
    This might just solve your problem (I've never gotten the "Expires" header to do what I want). It causes the CGI script to be treated the same as an HTML page for purposes of caching.
    print "Content-type: text/html\nCache-Control: public\n\n";
Re: Data Missing?
by lhoward (Vicar) on Apr 11, 2000 at 18:25 UTC
    This is slightly off-topic, but I never understood why all browsers to realod pages when they print them. Logically the browser should already have everything it needs to print it (since it already rendered the page to the screen). What other info could hte browser possibly need to print the page that it wouldn't have already (unless the browser is poorly designed and while the page is being viewed it has already forgotten how it rendered it).
    Les Howard
    www.lesandchris.com
    Author of Net::Syslog and Number::Spell

      Some do, some don't. Some even reload the page when you try to view source (!). Ideally, it should pull everything from its cache, unless the cache has been set to zero. As to why, it's just the way the browser coders decided to do it. I can't think of any obvious rational as far as printing except for the fact that it might be easier in their code to call the page then to retreive it from cache. I work on Mozilla, and this bug came up a while ago - Mozilla will be smart enough now to know when to load from cache, and when to reload the page.

      FWIW, I think the first answer that chromatic gave is the best solution (not returning a form, just an html page.

        I find it helpful to return the values as text, but use a bit 'o Javascript to fake a hyperlink on some of the returned text. (Clicking the link calls a Javascript function to Submit the value clicked. That way you can have a form submission with no Submit button, it just looks like a hyper-link.) This is useful when you are returning your results as text, but want some interactivity functionality.