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

I need to display the checked boxes from form input.
This code works, but I was looking for something more elegant.
Any suggstions?
if ($FORM{'h01'}) {$checked01 = " CHECKED "} if ($FORM{'h02'}) {$checked02 = " CHECKED "} if ($FORM{'h03'}) {$checked03 = " CHECKED "} if ($FORM{'h04'}) {$checked04 = " CHECKED "} if ($FORM{'h05'}) {$checked05 = " CHECKED "} if ($FORM{'h06'}) {$checked06 = " CHECKED "}

Replies are listed 'Best First'.
Re: There's got to be a better way?
by Chady (Priest) on May 19, 2001 at 10:51 UTC

    You should be using CGI.pm instead.

    for this example here, you might want to store these values in another hash an array...(it's dumb but it works)

    for $i (1..6) { $checked[$i] = 'CHECKED' if $FORM{"h0$i"}; }
    next if you want to check wether h04 is checked, you use $checked[4]

    just an idea

    Updated: did I say hash?
    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
Re: There's got to be a better way?
by Dominus (Parson) on May 20, 2001 at 02:48 UTC
    What I don't understand is why you need this code at all. Several people suggested alternatives involving hashes and arrays and for loops and so on, but it seems to me that that misses the point, and that these six lines are completely unnecessary. From the way you're using $FORM{'h04'}, I can see that it is basically a boolean variable, with a true value if a certain box was checked, and a false value if not. What's the point of creating a second variable to record exactly the same information?

    For example, if you're planning to test the $checked variables later, like this:

    if ($checked03) { ... }
    then you can just do this instead:
    if ($FORM{'h03'}) { ... }
    If you're planning to print out the word CHECKED then why not put that logic in the place where the message is printed, instead of creating six separate variables beforehand?

    --
    Mark Dominus
    Perl Paraphernalia

      Printing the word CHECKED is exactly what I want to do. I find it to be simpler to have the variables set in advance as opposed to incorporating them into my print statements which would then require an alternative else statement. Also, dealing with them before hand makes reusing the code in another application down the road a whole lot easier.
        Printing the word CHECKED is exactly what I want to do. I find it to be simpler to have the variables set in advance as opposed to incorporating them into my print statements which would then require an alternative else statement.
        If you arrange things correctly, it will all work out right---which was what you were asking for. Change your HTML form to contain HTML code like this:
        <input type=checkbox name='h04' value=' CHECKED '>
        Now when the user checks the checkbox, $FORM{'h04'} will automatically contain the value you want, CHECKED. Then to print out your report, you just say:

        print "Box 4: $FORM{h04}";
        You no longer need the extra variables, because the form variables get the correct values in the first place. No else required.

        --
        Mark Dominus
        Perl Paraphernalia

Re: There's got to be a better way?
by srawls (Friar) on May 19, 2001 at 18:02 UTC
    Here, you need to substitute $numOfTries for however many form elements you want to check.
    $FORM{"h0$_"} and $checked[$_] = 'CHECKED' for 0 .. $numOfTries;


    The 15 year old, freshman programmer,
    Stephen Rawls