I think CGI.pm could do it for you, it has parameters to auto-fill in submitted values (not only checkboxes). You should read the documentation for it.

But just focusing on your code, you could simplify it a lot. First, use a subroutine. (Note that the description for a checkbox should follow the checkbox, but I made it fit your code)

sub checkbox { my ($name, $value, $sel) = @_; my $checked = ($value eq $sel) ? "checked" : ""; print <<EOT $value: <input type="checkbox" $checked name="$name" value="$value">&nbsp;&nbsp; EOT return ($value eq $sel); }

This way, you can write your first two if statements like this:

checkbox("ss", "ss", $ss); checkbox("ss", "ssd", $ss);

Next, you could replace the first three if statements by

my $found = 0; CHECKBOX: foreach (ss ssd pers) { if (checkbox("ss", $_, $ss)) { $found++; last CHECKBOX; } } if (not $found) { # code for those checkboxes with special treatment }

Of course, the code could be more simplified, but I leave that as an exercise to the reader. ;-) (No, actually, you will have to do it yourself because I don't know your problem.)


In reply to Re: Edit page with checkboxes by crenz
in thread Edit page with checkboxes by perleager

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.