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

Hopefully this is within your wisdom. I have a CGI script that when accessed from a web browser, loads file data into form input fields. After the user modifies the fields and presses the submit button, the new values are written back into the file. SImple. But I want more. I want the screen to refresh with the new values populated in the form input fields. My approach was to try redirecting to the same page. Instead, my code (below) seems to bring up the cached values and write those to the file the second submit. Is there a way I can get the whole script to start over, and read the values in from the newly written file? Thanks.
#!/usr/local/bin/perl use CGI qw/:standard/; my $new_cgi = new CGI; my $i=0; $| = 1; # no more buffering my $full_url = $new_cgi->url(); print header, start_html('Pattern editor'), h1('Pattern Edit Center'), start_form; open PATS, "pats.txt" or die "cannot open pats.txt\n"; while (<PATS>) { if (/^\#.*/) { chomp; print hidden(-name=>"key$i", -value=>" +$_"); $i++; next; } s/#.*//; # strip trailing comme +nts next unless /\S/; # skip blank lines my ($key, $val) = split /\t+/; $val = $key unless $val; print "<TR><TD><input type=text name=key$i siz +e=13 value=$key> </TR>\n"; print " <TD><input type=text name=val$i size=2 +4 value='$val'> </TD></TR>\n"; $i++; } close PATS; # use old style for now - clean up later... print "<TR><TD><input type=text name=key$i size=13 > < +/TR>\n"; print " <TD><input type=text name=val$i size=24 > </T +D></TR>\n"; print submit, end_form, hr; if (param()) { open PATS,">pats.txt" or die "Cannot open pats.txt\n"; my $j = 0; while ($j <= $i) { print PATS param("key$j"), "\t", param("val$j") . "\n"; $j++; } close PATS; # Now that I've written the file, I should be able # to refresh and read the values back in. But How? print redirect($full_url); # for debugging purposes, print the form values to the bottom of page $j=0; while ($j <= $i) { print em(param("key$j")), " ", em(param("val$j")),br; $j++; } hr; }

Replies are listed 'Best First'.
(jeffa) Re: CGI refresh w/a twist
by jeffa (Bishop) on Jul 25, 2003 at 01:46 UTC
    If you want the values that are in the text field's to be there after the form is submitted, then you can do so by using CGI::textfield() to create the <input/> for you. Something like:
    print "<TR><TD>", textfield(-name=>"key$i", -size=>13, -default=>$key), "</TR>\n" ;
    Should do the trick. Addtionally, if you need to turn off this 'stickieness', add the override attribute and set it to one:
    textfield(-name=>"key$i", -size=>13, -override=>1)
    Hope this helps. :)

    Oh yeah ... if you import the :standard pragma:

    use CGI qw/:standard/;
    Then you don't need to instantiate a CGI object yourself. Using :standard is using what is called the procedural interface - the methods are imported into your script's namespace. Actually instantiating an object is using what is called the object oriented interface. You should pick one and stick with it - which one you pick depends on what you need or what you have already coded. Since you have used the proceedural more than the OO, just remove this line:
    my $new_cgi = new CGI;
    and change the next line and only line that uses $new_cgi to:
    my $full_url = url();

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Jeffa, That did the trick. Thanks very much. -Michael
Re: CGI refresh w/a twist
by rupesh (Hermit) on Jul 25, 2003 at 09:46 UTC
    I was in the same situation a couple of months back.
    I suppose you would like to take a look at this thread and see whether any of the suggestions are helpfull.


    we're born with our eyes closed and our mouths wide open, and we spend our entire life trying to rectify that mistake of nature. - anonymous.