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

Dear Monks,

I'm writing a CGI form that I want to behave differently based on the value of a URL query string (e.g. http::/localhost/cgi-bin/test_form?url_group=Boston3). Once the form has been filled out, I want to provide a button to let the user start over, while still maintaining the original param from the URL. I have tried using $query->hidden('url_group') to pass the URL param along until the last part of the script in which I have essentially:

my $url_group = $query->param('url_group'); print start_form(-action=>"test_form.pl?$url_group"); print $query->defaults; print end_form;
But this doesn't work. The script just gets called as if $url_group were undefined. Below I have a simple test case:
use strict; use warnings 'all'; use CGI qw(:standard); my $query = CGI->new(); my $url_group; if ($query->param('url_group') =~ m/^(Boston[1|2|3])$/) { $url_group = 'url_group=' . $query->param('url_group'); } print header, start_html; print "<p><b>\$query->param('url_group') = </b>\"" . $query->param('ur +l_group') . "\"</p>"; print p("<b>The value of \$url_group is:</b> $url_group"); print start_form(-action=>"test_form.pl?$url_group"); print $query->defaults("This doesn't pass the group"); print end_form; print end_html; print p("<b><a href=test_form.pl?$url_group>This does work</a></b>") i +f ($url_group);
Even if I refresh the browser with the query string still in the URL, it doesn't get passed the param. (I have to browse the script first *without* the query string, and then browse again *with* the query string, at which point it the param gets used initially.) Interestingly, a *link* to the script using the query string does work...but I'd rather use a button in the real script. Any idea how I can get this to work?

Replies are listed 'Best First'.
Re: Can't seem to pass query string with start_form(-action=>"test_form.pl?$url_group")
by Sinistral (Monsignor) on Feb 11, 2008 at 02:18 UTC

    When you use the CGI code you posed, the initial HTML generated does this:

    <form method="post" action="test_form.pl?" enctype="multipart/form-data">
    <input type="submit" name=".defaults" tabindex="1" value="This doesn't pass the group" />
    </form>

    So what youre getting is a form submitted via post, that contains a submit button with name .defaults and a value of "This doesn't pass the group". The fact that it's a post partly explains why you aren't getting the values; you don't have an HTML element that the default can be applied to. I think you might have been relying on this section of the CGI documentation:

    print defaults('button_label')

    You don't need that, because that code creates a button that generates all the default values for the HTML elements, like lists, radio buttons, and the like.

    You also don't want to put a question mark and a value after the action name, because that's an (incorrect) attempt at a get type query, where you have script_name?var1=val1&var2=val2 and so forth

    print $query->hidden(-name => 'url_group', -default => $query->param('url_group'));

    should be what you want. And make the action just be test_form.pl. Hope that helps. And for the button you want, just use:

    print $query->submit('This does pass the group');