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

Dear Monks, I guess, I don't expect CGI pm to act this way, but maybe I'm missing something. Is textfield function doing something totally weird on HTML it emits? Please take a look at following code... Thanks.
use warnings; use strict; use CGI qw(-no_xhtml :standard *table *Tr ); print header, start_html, start_form, start_table; print "CGI.pm Version: $CGI::VERSION"; print hr; print h5("HTML Input tag based counter increments"); print input({type => 'text', name => 'val', value => param('val') + 10 }); print hr; print h5("CGI.pm textfield based counter fails to increment"); print textfield({type => 'text', name => 'val2', value => param('val2') + 10 }); print submit(); print end_table, end_form, end_html;

Replies are listed 'Best First'.
Re: CGI.pm input vs textfield
by blokhead (Monsignor) on Jun 23, 2004 at 19:08 UTC
    You need to add force => 1 to the textfield.

    input is one of CGI.pm's "dumb" tag creation methods. None of the attributes you pass ("type", "name", "value") have any special meaning -- it just generates the HTML blindly. You can even write things like this: it's all the same to the input method:

    input({ foo => "bar", blok => "head" }); # <input foo="bar" blok="head" />
    textfield, on the other hand, is a "smart" tag creation method with special logic. Some attributes you pass in now have special meaning. The kicker is that it inteprets the "value" attribute as merely a default (In fact, you can use "default" as an alias for "value"). So unless you tell it otherwise (with force => 1), it will act as a "sticky" form and take the new value for that field from the query string.

    blokhead

      If you want to turn off sticky forms on all fields (as setting force => 1 for each field can get irritating if your form is large), you can do it in the use statement

      use CGI qw(-no_xhtml :standard *table *Tr -nosticky);

        Tried -nosticky it makes no difference. Now force => 1 does make it work "as expected". To me, force => 1 needs to be a default behaviour. Thanks for your good advice.
Re: CGI.pm input vs textfield
by bassplayer (Monsignor) on Jun 23, 2004 at 17:22 UTC
    I don't believe that there is a 'type' attribute for the textfield() method. 'text' is the only type of <input> widget that textfield() creates. Here is a link that offers more info.

    bassplayer