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

Dear Monks,

I'm wondering if someone could help me understand why the following doesn’t work.

I have a CGI script that displays different contents depending on whether or not the “Submit” button has been clicked. The CGI script works fine by itself, but when I attempt to include it in a .shtml file by way of an SSI on Apache 2.2.6, it doesn’t work. After clicking the submit button, the original form just redisplays as if the button had never been clicked. I get the same results with both of the following:

<!--#include virtual="/cgi-bin/choose_color.cgi"--> <!--#exec cgi="/cgi-bin/choose_color.cgi"-->

On the other hand, simple SSIs (such as to display the date, or a visitor counter CGI script) work fine. I have also tried using <FRAME> tags in a separate .html file to put the CGI in it’s own frame…..this works as long as it’s the only frame. As soon as I add another, the CGI form no longer displays at all.

I wanted to do this because the HTML file already exists with all the proper formatting, and I thought I could just rename it to .shtml and include the CGI. So, I’m wondering the following:

1) Are the results I’m seeing with the .shtml files expected? If so, could you help me understand why?

2) What would be a better approach for what I want to do?

My test case is below:

choose_color.shtml
<html> <head> <title>Choose Color</title> </head> <body> <p><!--#exec cgi="/cgi-bin/choose_color.cgi"--></p> </body> </html>
choose_color.cgi:
#!c:/perl/bin/perl.exe use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use strict; use warnings; print header, start_html, h3("Choose Color"), hr; my $query = CGI->new(); if ($query->param('Submit')) { # If user has chosen a favorite color print p("You chose <b>", $query->param('color'), "</b>.\n"); } else { # User hasn't yet chosen a favorite color print p("Choose your favorite color:\n"); print start_form; print p($query->popup_menu(-name=>'color', -values=>[qw(blue green + red gold yellow)]), $query->submit("Submit")); print end_form; } print end_html;

Thank you,
memnoch

Replies are listed 'Best First'.
Re: CGI form doesn't work by way of SSI
by merlyn (Sage) on Jan 23, 2008 at 14:04 UTC
    Look at the generated HTML. My guess is that the form's action URL is still the original HTML url, not your form CGI. You'll need to add a parameter to your start_form() call to get the proper action URL.
Re: CGI form doesn't work by way of SSI
by perrin (Chancellor) on Jan 23, 2008 at 16:07 UTC
    I don't think the query parameters are passed to includes. Try dumping out the query string in your CGI to check. I'd suggest calling your CGI directly and having it include the header and footer. You could even use SSI-like syntax for that, with one of the SSI modules on CPAN.
      If you use Apache2 then you can even emit HTML that contains SSI directives, and let Apache process them.

      To enable that, you have to include a line like

      AddOutputFilter INCLUDES .cgi
      In your .htaccess or server config.
        Good point, I tend to forget that. That was a great new feature in apache 2.