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

Great Monks,

In my @mailid i have more than 10 mailid's, i have to show in CGI form using checkbox, I used this below code to

print map { qq{<tr><td><input type="checkbox" name="address" value="$_ +" /> $_</td></tr>} } @mailid;
It's working. In CGI form, checkbox i check more than one and click delete button, In another form i use param('address') I am getting only one value from the selected check box. I have to get all the checked box values to delete the mail id's from the database. please anyone help for the code.

Thanks in advance.

Replies are listed 'Best First'.
Re: How to take values from check box in CGI form?
by chromatic (Archbishop) on Jun 16, 2005 at 05:30 UTC

    I'm not sure I understand the question (given monarch's response). Does it help if I say that param() returns multiple values in list context? That is:

    my $message_id  = $q->param( 'address' );

    gives you only one message id, while:

    my @message_ids = $q->param( 'address' );

    gives you all of the checked values.

      I'm curious, because my guess was that the problem was that browsers don't send parameters for unchecked boxes. And the thread author wanted to delete based on which boxes were unchecked.

      Was this the problem, gube?

      Update: changed capitalisation of the name gube.

        A quick Google search reveals that you're right.

        If an element is selected, its value is returned; otherwise it is not.

        Useless trivia: In the 2004 Las Vegas phone book there are approximately 28 pages of ads for massage, but almost 200 for lawyers.
Re: How to take values from check box in CGI form?
by monarch (Priest) on Jun 16, 2005 at 04:34 UTC
    This is indeed a problem.. I've encountered it myself after creating half a zillion checkboxes and then wanting to perform delete operations on the unchecked fields.

    There may be better solutions, but one way I might approach this is to create (for every checkbox) a hidden field with a similar name to the name assigned for the checkbox. If my checkbox name was name="checkbox_284" then I'd name my hidden field name="hidden_checkbox_284".

    In my CGI form I'd have a loop that goes something like:

    foreach my $param ( $cgi->param ) { next if ( $param !~ m/^hidden_(.+)$/ ); my $checkboxname=$1; if ( ! $cgi->param( $checkboxname ) ) { # delete this item } else { # add this item } }

    update: So that, even if the browser doesn't send the checkbox parameters for the unchecked fields, at least the browser sends all the hidden fields and I can then use them to check for the existance of the checkbox field.

Re: How to take values from check box in CGI form?
by gube (Parson) on Jun 16, 2005 at 05:36 UTC

    No, Monarch i have to delete only the checked box.

    Ok. Thanks for both. It's working good. Thanks too much chromatic.

    Update: No, Monarch i have to delete only the checked box.

Re: How to take values from check box in CGI form? [OT]
by reasonablekeith (Deacon) on Jun 16, 2005 at 07:42 UTC
    as an aside, and a bit of a nit pick, I hate to see map used like this. IMHO it should only normally be used if you're processing an array and outputting an array.

    What's wrong with this?

    for (@mailid) { print qq{<tr><td><input type="checkbox" name="address" value="$_" +/> $_</td></tr>\n}; }
    ---
    my name's not Keith, and I'm not reasonable.

      I agree with your nit that map should be used to return an array (er, list), that is, I don't like map in void context. However, in this case, it is returning a list - which is then used as a list of parameters to print. Remember - print takes a list just fine ;-)