Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Form doesn't get updated after submission to same page

by Hue-Bond (Priest)
on Oct 17, 2006 at 10:06 UTC ( #578725=perlquestion: print w/replies, xml ) Need Help??
Hue-Bond has asked for the wisdom of the Perl Monks concerning the following question:

I don't know how to explain this problem, so let's try with an example:

This minimal CGI script shows a page with two paragraphs and a small form that contains a dropdown menu, a textbox and two buttons. The textbox is for illustrative purposes only. After selecting a value from the dropdown menu and clicking the button "Set", the page reloads and the paragraphs show the new value selected, but the textbox stays with the same content (which I don't expect; I think it should change too). Similarly, clicking on "Add" changes the paragraphs but neither the textbox nor the dropdown menu reflect the new value.

In the real world, the dropdown menus are for selecting a date (the three typical year, month and day) and the "Add" button is "Go to the next day". I would like that the menus' default options turned into that of the next day (in order to be able to click more times) but, as shown, they stay the same.

I thought some kind of persistence of $q caused by mod_perl would be related but I just tried without it and the problem didn't go away.

#!/usr/bin/perl use warnings; use strict; use CGI; my $q = CGI->new; my $num = $q->param ('num') || 1; my $button = $q->param ('go'); ## increment $num if clicked in "Add" $num++ if (defined $button and 'Set' ne $button); print $q->header ('text/html'), $q->start_html, "paragraph 1: num is [$num]", $q->start_form, $q->popup_menu (-name => 'num', -values => [1 .. 3], -default +=> $num), $q->textfield (-name => 'foo', -value => "num is [$num]"), $q->submit (-name => 'go', -value => 'Set'), $q->submit (-name => 'go', -value => 'Add'), $q->end_form, "paragraph 2: num is [$num]", $q->end_html;

David Serrano

Replies are listed 'Best First'.
Re: Form doesn't get updated after submission to same page
by iguanodon (Priest) on Oct 17, 2006 at 10:23 UTC
    Check out -nosticky and -override in the docs.

      This site is wonderful! Just 20 minutes! Pizza delivery takes longer!

      Check out -nosticky

      It doesn't work™. Replacing the use line with use CGI qw/-nosticky/; doesn't change anything. The documentation of -override mentions -no_sticky so I tried it too, to no avail (it must be a typo). Also tried $q->nosticky, $q->nosticky (1) and $CGI::NOSTICKY = 1 after some googling.

      and -override

      This solves the problem, although it has the drawback that I have to add it everywhere. Thank you!

      David Serrano

        Peaking at the source suggests that $CGI::nosticky(1) should work as well.

        You probably want to also force a refresh in your browser. Some/most browsers will remember the contents of fields for you as well so determining if its CGI or your browser can be annoying. Check the source produced by your code. If you are using -nosticky then it should be outputing the value you define in the script, otherwise it should be outputing the value last entered (/me thinks).

        Eric Hodges

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://578725]
Approved by planetscape
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2018-11-21 15:56 GMT
Find Nodes?
    Voting Booth?
    My code is most likely broken because:

    Results (244 votes). Check out past polls.