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

Hi,

when I have a form with several checkboxes like that:

<input type=checkbox name=lookup value="exact" checked> <% _('exact') +%><br> <input type=checkbox name=lookup value="partof"> <% _ +('part-of') %><br> <input type=checkbox name=lookup value="morph"> <% _( +'morphology') %><br> <input type=checkbox name=lookup value="fuzzy"> <% _( +'fuzzy') %><br> <input type=checkbox name=lookup value="regex"> <% _( +'regex') %>
When trying to process it under Mason I stumble about really kludgy processing: If none or only one checkbox is checked, $ARGS{lookup} behaves like a scalar where the value is the value of the checkbox.

BUT

If more than one checkboxes are checked, $ARGS{lookup} is a reference to an array. An ARRAY? Hell how should I know, which element index corresponds to what value? If I check exact and partof, $ARGS{lookup}[1] is "partof", but if I check exact and morphology, $ARGS{lookup}[1] is "morph".

There must be a better / working way to solve this!?

Bye
 PetaMem
    All Perl:   MT, NLP, NLU

Replies are listed 'Best First'.
Re: Mason and (multiple) checkbox processing
by shenme (Priest) on Oct 28, 2004 at 20:45 UTC
Re: Mason and (multiple) checkbox processing
by ikegami (Patriarch) on Oct 28, 2004 at 20:29 UTC
    You should probably give your boxes different names (name=partof, name=morph, etc). You don't even need to specify a value=.

    If not, you can fix it up in code as follows:

    my %lookup; { my $lookup = $ARGS{lookup}; if (ref($lookup)) { $lookup{$lookup} = 1; } else { $lookup{$_} = 1 foreach (@$lookup); } } print($_, ': ', ($lookup{$_}?"checked":"not checked"), $/) foreach (qw(partof morph fuzzy regex));

    There might be a better way, but I don't know Mason at all.

      You should probably give your boxes different names (name=partof, name=morph, etc)

      Bad idea IMHO. If you happen to combine a form out of several (mason) components and there is even a possibility, that someone in the future will add some components, you just pollute the namespace of the form that way. Of course, a programmer has to look at the code/docs before doing some extensions, but using same name different values, you reduce the number he has to cross-check from m * n to n.

      Bye
       PetaMem
          All Perl:   MT, NLP, NLU

Re: Mason and (multiple) checkbox processing
by PodMaster (Abbot) on Oct 29, 2004 at 09:43 UTC
    Hell how should I know, which element index corresponds to what value?
    Why would you want to know? Besides, CGI doesn't work like that (key value pairs is all you get).

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.