[ic] [Checked]

Mark Johnson interchange-users@lists.akopia.com
Thu Jun 14 09:55:13 2001


My turn :)

If I understand the problem correctly, it is that you want values true
by default, but false if the user has unchecked a given box and the page
reloads as the result of an error. The simple key is to set a unique
flag on that page. If the flag is present, then set the default values
according to what is in the CGI space (and specifically by what is not
in the CGI space); if not, set the values to true. 

Since you didn't specify that your checkboxes were the same or different
elements, it's now twice as difficult to answer your question. Precision
is your friend. For either case, the page should contain something like

<input type=hidden name=error_reset_flag value=1>

(1) Different elements:
Say your boxes are foo1, foo2, foo3. At the top of the page:
[calc]
   if ($CGI->{error_reset_flag}) {
     foreach (qw/foo1 foo2 foo3/) {
       $Values->{$_} = $CGI->{$_};
     }
   }
   else {
     @{ $Values }{qw/foo1 foo2 foo3/} = (qw/bar1 bar2 bar2/);
   }
   return;
[/calc]

With this configuration, you can simply use the
<input type=checkbox name=foo1 value=bar1 [checked foo1 bar1]>
syntax.

(2) Same elements:
Say your element is foo, and the three possible values are bar, baz, and
buz. Here, you should probably create your checkbox list dynamically to
make things easier. So, in place of your hard-coded checkbox list:

[calc]
   my ($out,$value_list,%labels);
   my @cbvalues = qw/bar buz baz/;
   @labels{ @cbvalues } = ('Bar Label', 'Buz Label', 'Baz Label');

   $value_list = $CGI->{error_reset_flag} ? $Values->{foo} : join ("\0",
@cbvalues);

   foreach (@cbvalues) {
      my $checked = '';
      $checked = ' checked' if $value_list =~ /\b$_\b/;
      $out .= qq!<input type=checkbox name=foo
value=$_$checked>$labels{$_}!;
   }
   return $out;
[/calc]
   
Both these snippets are untested, so I may have missed a little
something here or there. But, I believe the logic is what you want.     

cfm@maine.com wrote:
> 
> On Thu, Jun 14, 2001 at 12:49:33AM +0200, Stefan Hornburg (Racke) wrote:
> > Victor Nolton <ven@pragakhan.com> writes:
> >
> > > I have 9 <input type="checkbox">'s
> > > By default none are checked. So far so good.
> > > if the user clicks it and submits it, it is fine, if there is a error
> > > the page is displayed and they are given the errors of what they
> > > forgot to fill out. boxes are still checked appropriately.
> > >
> > > IF they unclick one of the 9 boxes (which aren't required) and they
> > > submit if the page is redrawn (cause of errors) the boxes are checked
> > > again.
> > >
> > > Checked the docs used some examples. no luck. I just need it so that
> > > if they uncheck a box it stays unchecked.
> > >
> > > Anyone have any snippets of code?
> > > checked reference http://interchange.redhat.com/cgi-bin/ic/dev/ictags_14.html
> >

-- 
Mark Johnson
Senior Systems Architect - Professional Services
Red Hat, Inc.
E-Business Solutions
markj@redhat.com
703-456-2912