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

Hi, I am trying to save parameters from a form using $query->save, but the first time the script is run, the parameters are not saved, for every subsequent call to the script they are saved. Is there some magic I am missing with this code?
use CGI; use HTML::Template; use HTML::FillInForm; use CGI::Carp qw(carpout); use Data::Dumper; our $q = CGI->new; my $TData = $q->Vars; # return value pairs from form carp "Form values:", Dumper($TData) if $debug > 1; if ($TData->{'Save'} || $TData->{'Submit'}) { if (-e $savefile) { open (SFILE, $savefile) || die "cant open $savefile for read\n"; $old = new CGI(SFILE); close SFILE; } } my $t = HTML::Template->new(filename => $tmplfile, option => 'loop_con +text_vars'); print $q->header; print $q->start_html(); $t->param(ROWS => \@Data); my $html = $t->output; my $fhtml = HTML::FillInForm->fill(\$html, $q); print $fhtml, "\n"; print $q->end_html; my $tmp = $q->Vars; # see if this forces the save to work first time t +hrough!! open(SAVEFILE, ">$savefile") || die "$pgm: cant open $savefile for wri +te\n"; $q->save(\*SAVEFILE); print SAVEFILE "$pgm: end of current state at $today\n"; close (SAVEFILE);

Replies are listed 'Best First'.
Re: CGI $query->save does not save data in first invocation of script
by Anonymous Monk on Jun 24, 2009 at 01:14 UTC
    Sorry, I forgot you might need to look at the template as well. Note that I am using the post method on the form.
    <form method="post"> <table width='100%' border="1" cellspacing='2'> <tr> <th colspan='2'>&nbsp;</th> <th class=nwright>Sun</th> <th class=aright>Mon</th> <th class=aright>Tue</th> <th class=aright>Wed</th> <th class=aright>Thu</th> <th class=aright>Fri</th> <th class=nwright>Sat</th> <th class=aright>Total</th> </tr> <TMPL_LOOP ROWS> <tr> <td><TMPL_VAR NAME=Client></td> <td><TMPL_VAR NAME=Job></td> <td class=nwright><TMPL_VAR NAME=Sun></td> <td class=aright><TMPL_VAR NAME=Mon></td> <td class=aright><TMPL_VAR NAME=Tue></td> <td class=aright><TMPL_VAR NAME=Wed></td> <td class=aright><TMPL_VAR NAME=Thu></td> <td class=aright><TMPL_VAR NAME=Fri></td> <td class=nwright><TMPL_VAR NAME=Sat></td> <td class=aright><TMPL_VAR NAME=Total></td> </tr> </TMPL_LOOP> </table> <table width='100%'> <tr> <td class=aleft><input type="submit" class=save name='Save' value= +"Save times"></td> <td class=acentre><input type="submit" class=submit name='Submit' +value="Submit times"></td> <td class=aright><input type="submit" name='Cancel' value='Cancel +all changes'></td> </tr> </table> </form>
Re: CGI $query->save does not save data in first invocation of script
by Anonymous Monk on Jun 24, 2009 at 09:01 UTC
    but the first time the script is run, the parameters are not saved, for every subsequent call to the script they are saved.

    How are you checking?

    #!/usr/bin/perl -- use strict; use warnings; use autodie; use CGI; use Shell::Command; my $cgifile = __FILE__.'.txt'; { open my $out, '>', $cgifile; my $q = CGI->new({1..10}); $q->save($out); } cat $cgifile; { open my $in, '<', $cgifile; my $q = CGI->new($in); print $q->Dump; } __END__ 1=2 3=4 7=8 9=10 5=6 = <ul> <li><strong>1</strong></li> <ul> <li>2</li> </ul> <li><strong>3</strong></li> <ul> <li>4</li> </ul> <li><strong>7</strong></li> <ul> <li>8</li> </ul> <li><strong>9</strong></li> <ul> <li>10</li> </ul> <li><strong>5</strong></li> <ul> <li>6</li> </ul> </ul>
    Maybe its this bug #13158: empty name/value, when saved, prevents proper restore from filehandle.
Re: CGI $query->save does not save data in first invocation of script
by Anonymous Monk on Jun 25, 2009 at 06:40 UTC
    How do I check: I look at the contents of the $savefile after each invocation.