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

I'm working with perl::cgi and I'm having a couple issues.

I'm trying to use a hidden param lets call it "current" to control the flow on my page. When I insert this parameter with a different value in different cgi forms it defaults to it first assigned value in the html generated.

Example:
push(@buffer, $cgi->start_form(-method => 'post' , -action => 'example +.cgi'), $cgi->submit(-name => 'edit_notes', -value=>$timestamp, -la +bel =>"edit"), $cgi->hidden(-name => 'user', $user), $cgi->hidden(-name => 'current', -value=>'example'), $cgi->end_form);
When I create another form later on in the page and have an alternate value for 'current' it is replaced in the html by 'example'.

The other issue I am confused about which may or may not be related is that in the html generated by the cgi forms it is including multiple copies of the same parameters.

The above code generates this:
<input type="hidden" name="user" value="UserID" UserID /> <input type="hidden" name="user" value="UserID" UserID /> <input type="hidden" name="user" value="UserID" UserID /> <input type="hidden" name="user" value="UserID" UserID />
Any help would be appreciated.

Replies are listed 'Best First'.
Re: cgi issues
by Joost (Canon) on Jun 26, 2007 at 19:17 UTC
    CGI form field generators fill in the current values of those fields (i.e. the values as they are in the param() method - normally the user input) if those values exist.

    You can override those values by setting the param before creating the form fields:

    $cgi->param("current","something else");
    Also, you specify name/value pairs for hidden fields using either hidden($name,$value) or hidden(-name => $name, -value=>$value). Mixing them, as you're using for the user field does not work.

    Also also, that code does not print anything, and if it does did it wouldn't print what you're claiming:

    use CGI; use strict; use warnings; my $cgi = CGI->new; my @buffer; my $timestamp = time; my $user = "Me"; $cgi->param("current","something"); # set "current" push(@buffer, $cgi->start_form(-method => 'post' , -action => 'example +.cgi'), $cgi->submit(-name => 'edit_notes', -value=>$timestamp, -la +bel =>"edit"), $cgi->hidden(-name => 'user', $user), $cgi->hidden(-name => 'current', -value=>'example'), $cgi->end_form); print @buffer;
    output:
    <form method="post" action="example.cgi" enctype="multipart/form-data" +> <input type="submit" name="edit_notes" value="edit" /><input type="hid +den" name="user" value="" me /><input type="hidden" name="current" va +lue="something" /></form>
    Note the problem caused by the wrong arguments in hidden( -name => 'user', $user)