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

Fellow monks,

I've got a reentrant cgi program, part of which is a form. When I error check the form to make sure all the required fields are filled in I'm using the fact that I can just call my CGI program again.

The rub comes if the user hasn't filled in all the required fields. After all required fields are filled in, the program moves to the next web page as it should, however it's concatenating the information in the field to the data that exists for the given field: e.g. if the name entered is Joe, it becomes JoeJoe. This is only happening on fields that were previously entered. If there are no errors, then all works as expected.

I tried using the override function on the fields but that returned "error" in the one field I tested it in. My next thought was to delete the paramters of the required fields that were filled in using $query->delete() but that doesn't make sense as a solution.

So my question is why is this happening? Has anyone else expereinced this happening? And, if so how did you fix it?

Update: Thanks to the other monks for pointing out I didn't provide enough information. I'd been fighting something I thought was fixed and banged this question out. Bad idea.

I'm using CGI.pm to do my form building and processing. Like I said above, I'm calling the same CGI script but using different subroutines depending on what parameters are passed through the submit.

On this particular section I'm trying to verify that all the required fields were filled in, and if not, the page is called again with the errant fields highlighted. All very simple.

print $query->br, $query->br, $query->br, $query->startform; --code snipped-- $query->TR( [ $query->td( { -class => "$bData{BillName}{value}" }, $query->font( { -size => '-1', -face => 'arial, helvetica' }, 'Name:' ) ) . $query->td( $query->textfield( -name => 'BillName', -value => '', -maxlength => 70, -size => 50, ) ) ] ), # end TR

is how I've set up my form. Obviously there are more fields, but until I tested for a blank field, I never realized that the form data is being concatenated to itself for every field.

I had changed the code to

$query->textfield( -name => 'BillName', -value => $query->param('BillName'), -maxlength => 70, -size => 50, )

however this places the word "-maxlength" in the field upon reentry.

As I said, using -override in the textfield placed the word "error" in the form when it was redrawn. My understanding of -override was that you could change the vaule in a field, but that's not the answer either.

Using Data::Dumper to dump the passed paramters is showing that every time a correction is made, all the information that was filled in before is concatenated to it's field, so that if the BillName field was Joe Bloe, then it's passing Joe BloeJoe Bloe if only one correction was done.

Hopefully I've made this understandable enough so that perhaps someone can spot my problem.

Update II: I've finally solved the problem!

In the middle of working on some error checking I had a line that passed all of the parameters back to the form in hidden fields, thereby duplicating all information contained in the form.

Since I was passing essentially two copies of the same parameter, when the code re-entered the subroutine, it was tacking on the hidden to the sticky, and hence the problem.

Revolution. Today, 3 O'Clock. Meet behind the monkey bars.

Replies are listed 'Best First'.
Re: Problem with data fields duplicating in reentrant CGI form
by imp (Priest) on Jul 20, 2006 at 05:02 UTC
    I'd recommend against doing this:
    $query->textfield( -name => 'BillName', -value => $query->param('BillName'), -maxlength => 70, -size => 50, )
    If there is no parameter named BillName then param('BillName') will return; without a value, which causes the hash to have this structure:
    $query->textfield( -name => 'BillName', -value => -maxlength, 70 => -size, 50 => undef, )
    You can confirm with this example code:
    #!/usr/bin/perl use strict; use warnings; use CGI; use Data::Dumper; my $cgi = CGI->new; my %data = ( a => $cgi->param('a'), b => $cgi->param('b'), ); print "Content-type: text/plain\n\n"; print Dumper(\%data);
Re: Problem with data fields duplicating in reentrant CGI form
by imp (Priest) on Jul 20, 2006 at 05:18 UTC
    I tested the override flag and it worked for me.
    #!/usr/bin/perl use strict; use warnings; use CGI; use Data::Dumper; my $cgi = CGI->new; print "Content-type: text/plain\n\n"; print $cgi->textfield( -name => 'BillName', -value => 'fake', -override => 1, -maxlength => 70, -size => 50, );
    If you want to preserve the user parameters in the HTML you don't need to specify -value => 'fake', as CGI will automatically fetch the value for the field.
    When only one value is submitted these are synonymous:
    print $cgi->textfield( -name => 'BillName', -value => $cgi->param('BillName'), -maxlength => 70, -size => 50, ); print $cgi->textfield( -name => 'BillName', -maxlength => 70, -size => 50, );
    However, if multiple values are sent for the same name, then CGI::param() will return the list of values, which has a similar effect as my previous post.

      The override flag works for me as well, as I'm using it elsewhere to change the value of hidden fields. But it isn't working to replace what the paramter was prior.

      Revolution. Today, 3 O'Clock. Meet behind the monkey bars.
Re: Problem with data fields duplicating in reentrant CGI form
by imp (Priest) on Jul 19, 2006 at 23:53 UTC
    Please provide a minimal working example that demonstrates the problem

    Include the very minimal perl and html required to reproduce the issue.

Re: Problem with data fields duplicating in reentrant CGI form
by Herkum (Parson) on Jul 20, 2006 at 03:14 UTC

    Without having any idea of what you are working with you make it hard to give you an good advice.

    The only thing I can tell is that you are probably using a system which will autofill a form that did not pass validation while you are also filling in the form again, maybe by populating the template?

    Include the modules, as a minimum, that you are using how you are generating a web page and someone will be able to help you

Re: Problem with data fields duplicating in reentrant CGI form
by imp (Priest) on Jul 20, 2006 at 05:23 UTC
    If you provide a minimal implementation that is runnable and demonstrates the problem we'll have an easier time identifying the cause.

    As it stands I have no idea what your validation code is doing, and whether it uses a homegrown solution, Data::FormValidator, or some other option.