Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

CGI: stickier than I thought

by alien_life_form (Pilgrim)
on Nov 05, 2002 at 07:49 UTC ( [id://210405]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings,

Consider:

DB<1> use CGI DB<2> $q=new CGI; DB<3> p $q->hidden(-name=>'fubar',-value=>1) <input type="hidden" name="fubar" value="1" /> DB<4> $q->param(-name=>'fubar',-value=>3) DB<5> p $q->hidden(-name=>'fubar',-value=>1) <input type="hidden" name="fubar" value="3" /> DB<6> p $q->hidden(-name=>'fubar',-value=>1,-override=>1) <input type="hidden" name="fubar" value="1" />
I wonder if this is intended behaviour. The docs say that the parameters value will be preserved between invocations, and that the default value will not be used unless the parameter itself has never had a value.

In the above example, however, the current parameter value takes precedence even on the explicitely stated intention to change it... it sure looks counter-intuitive to me.
Cheers,
alf


You can't have everything: where would you put it?

Replies are listed 'Best First'.
Re: CGI: stickier than I thought
by rinceWind (Monsignor) on Nov 05, 2002 at 12:31 UTC
    Hi, I've been bitten by this one recently too.

    From perldoc CGI:

    Parameters:

    The first argument is required and specifies the name of this field (-name).

    The second argument is also required and specifies its value (-default). In the named parameter style of calling, you can provide a single value here or a reference to a whole list

    Fetch the value of a hidden field this way:

    $hidden_value = $query->param('hidden_name');
    Note, that just like all the other form elements, the value of a hidden field is ``sticky''. If you want to replace a hidden field with some other values after the script has been called once you'll have to do it manually:
    $query->param('hidden_name','new','values','here');
    The value you supply to hidden via -value is a default value. In order to pass it in, you need to set it via param, or create another query object with nothing set.
    my $q_unset = CGI->new(''); print $q_unset->hidden(-name=>'fubar',-value=>1);
Re: CGI: stickier than I thought
by dingus (Friar) on Nov 05, 2002 at 09:14 UTC
    Having three parameters of the same name in the same form on a page will (IIRC and I can't be bothered to verify so RTFM) create an ARRAY with 3 elements. So therefore nothing that occurs in your code affects the variable stickiness.

    Now if you were creating the form using the first 3 lines, then on submission outputting the 4th and 5th and then on resubmission outputting 6 and 7 I would be surprised because in that case the stickiness would almost certainly mean that 4 and 5 would output ..value="1".. not ..value="3"..

    Dingus

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://210405]
Approved by rob_au
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-03-28 13:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found