in reply to Re: Saving and storing form params with cgi.pm
in thread Saving and storing form params with cgi.pm

Many, many thanks to arturo for his patience in helping me figure this out

I had to create to query objects. Not knowing OO, I don't quite understand what's going on here but it works. Here is the code that works:
#!/usr/bin/perl -w use strict; use CGI qw(:html3); open ('QUERY', '<query.txt') || die "Cannot open: $!"; my $q = new CGI ('QUERY'); my $p = new CGI; close QUERY; BEGIN { use CGI::Carp qw(carpout fatalsToBrowser); open (LOG, ">errorlog.txt") or die ("Unable to open errorlog.txt: $!\n"); carpout(\*LOG); } print $q->header(); print $q->start_html(); print $q->startform("post",'eventform.cgi'); print $p->textfield(-name=>'field_name', -size=>50, -maxlength=>80); print $q->submit(-name=>'button_name', -value=>'value'); print $q->endform; print $q->end_html(); open (QUERY, '>query.txt') || die "Cannot open: $!"; $p->save('QUERY'); close QUERY;

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar";
$nysus = $PM . $MCF;

Replies are listed 'Best First'.
Re: Re: Re: Saving and storing form params with cgi.pm
by arturo (Vicar) on May 25, 2001 at 18:00 UTC
    OK, I'm assuming that what you want this script to do is maintain state in between invocations, and to allow users to change that state by submitting the form. Your code isn't (yet) able to do that. There are a couple of things that aren't clear to me, such as how exactly you expect this script to be accessed (can users stay on the form and keep resubmitting? I take it that's what you want to allow). But here's how I'd do the basic state-maintenance thing; note there's no code, because, as bjelli points out, the problem all along was with your program logic, which tells me that's what you need to think about first here.
    1. Get the parameters that have been submitted via HTTP.
    2. If the parameter set is blank, read the state in from the file. (using the method you're using)
    3. If the parameter set came from a form submission and not from the file, save the current set of parameters to the file.
    4. Present the form, with each field containing the relevant value from the parameter set that we have.

    If that's not what you wanted to do, then you'll need to be more explicit.

    The idea here is that when users come to your CGI for the first time, they'll receive the saved state. If they then submit the form, what they submit will get saved to the file.

    HTH