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

I have an SQL database that i pull info from and is parsed to a table. Beside each row i need to generate a delete check-box. When checked and sent i need the script to run a sub routine for each check box checked.
<input type=checkbox name=delete_this value=$oid>

The oid value is different for each...of course. I'm not sure at all what to do. Do i need the Form that is generated to give a unique name for each check box, then have the script determine each and process it? That sounds like a long way of doing it.

Replies are listed 'Best First'.
Re: Multiple Check boxes and Handling
by Masem (Monsignor) on Oct 06, 2001 at 02:04 UTC
    Yes, you need to give each checkbox a unique name. However, checking each one isn't as bad as it sounds. Set the name to something like "delete_this_$oid", as you'll see in a second.

    Assuming you can pull the values from the SQL db in an array ( @oid_list ) in the script that parsed the form, you can check the checkbox status of each one as so:

    foreach my $oid ( @oid_list ) { if ( $cgi->param( 'delete_this_$oid' ) eq 'on' ) { # do deleteion stuff } }

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    It's not what you know, but knowing how to find it if you don't know that's important

Re: Multiple Check boxes and Handling
by chromatic (Archbishop) on Oct 06, 2001 at 02:39 UTC
    With all due respect to Masem, you don't need to give each checkbox a different name. If you use CGI.pm to grab your parameters, param() in list context will return the values for *all* checked checkboxes.

    That is, if you have a bunch of checkboxes all named "delete_this" with different values, you can say:

    my $q = CGI->new(); my @deletable = $q->param('delete_this'); delete(\@deletable);
    ©
Re: Multiple Check boxes and Handling
by tachyon (Chancellor) on Oct 06, 2001 at 02:45 UTC

    All you need to do is name them all say "delete_this_value" and set the $oid value as their unique identifier just as you do in your example. You can then use CGI.pm thusly:

    use CGI; my $q = new CGI; @deletes = $q->param('delete_this_value'); for (@deletes) { # you get the value of $oid here for all checked boxes # etc }

    The vales of $oid for each checkbox will be stored in @deletes

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Looks good. One thing in need to do. I need to put each oid value into a separate variable so i can repeat the delete subroutine once per value.

        They are in separate variables in the @deletes array.

        $deletes[0] # contains first $oid to delete $deletes[1] # contains second $oid to delete ... $deletes[n] # contains n+1 th .....

        Here is how you use your array. Assunimg that $oid contains a filepath to a file you want to delete this will do that.

        <code> #/usr/bin/perl -w use CGI; use CGI::Carp('fatalsToBrowser'); use strict; my $q = new CGI; my @deletes = $q->param('delete_this_value'); print "Content-type: text/plain"; foreach my $oid (@deletes) { # you get the value of $oid here for all checked boxes &do_delete($oid); } sub do_delete { my $oid_to_delete = shift; unlink $oid_to_delete or die "Can't unlink $oid $!\n"; print "Deleted $oid_to_delete\n"; }

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print