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

Hi Monks!

I am working on this program that will print to the browser a grid of data rows from this database table, and I will have to select one or more rows and send the selected row(s) to this other program to process the changes. I am having a hard time on how after clicking on the checkbox(s) submit the corresponded row or rows of data to my other Perl program.
I am including a sample table with a data grid of what I am trying to accomplished with this (don't consider the JS to select all to work), I hope someone has done such thing that can show me how I can get this problem done.
Here is the code:

#!/usr/bin/perl use strict; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); my $q = new CGI; print header(); print <<HTML; <div style="width: 750px;"><form name="form" method="get" action="get_ +data_rows.pl"> <table class="GridView" id="GridView1" style="border-collapse: col +lapse;" rules="all" border="1" cellspacing="0"> <tbody> <tr class="Pink"> <th scope="col" align="left">Select All <span title="Click here to select/deselect all + rows"> <input id="" name="ctl0" type="checkbox"></sp +an> </th><th scope="col">Company Name</th><th scope="c +ol">City</th><th scope="col">Country</th> </tr><tr title="Click to toggle the selection of this row"> <td> <input id="" name="ctl1" type="checkbox"> </td><td>Alfreds Futterkiste</td><td>Berlin</td><t +d>Germany</td> </tr><tr title="Click to toggle the selection of this row"> <td> <input id="" name="ctl2" type="checkbox"> </td><td>Ana Trujillo Emparedados y helados</td><t +d>México D.F.</td><td>Mexico</td> </tr><tr title="Click to toggle the selection of this row"> <td> <input id="" name="ctl3" type="checkbox"> </td><td>Antonio Moreno Taquería</td><td>México D. +F.</td><td>Mexico</td> </tr><tr title="Click to toggle the selection of this row"> <td> <input id="" name="ctl4" type="checkbox"> </td><td>Around the Horn</td><td>London</td><td>UK +</td> </tr><tr title="Click to toggle the selection of this row"> <td> <input id="" name="ctl5" type="checkbox"> </td><td>Berglunds snabbköp</td><td>Luleå</td><td> +Sweden</td> </tr><tr title="Click to toggle the selection of this row"> <td> <input id="" name="ctl6" type="checkbox"> </td><td>Blauer See Delikatessen</td><td>Mannheim< +/td><td>Germany</td> </tr><tr title="Click to toggle the selection of this row"> <td> <input id="" name="ctl7" type="checkbox"> </td><td>Blondesddsl père et fils</td><td>Strasbou +rg</td><td>France</td> </tr><tr title="Click to toggle the selection of this row"> <td> <input id="" name="ctl8" type="checkbox"> </td><td>Bólido Comidas preparadas</td><td>Madrid< +/td><td>Spain</td> </tr><tr title="Click to toggle the selection of this row"> <td> <input id="" name="ctl9" type="checkbox"> </td><td>Bon app'</td><td>Marseille</td><td>France +</td> </tr><tr title="Click to toggle the selection of this row"> <td> <input id="" name="ct20" type="checkbox"> </td><td>Bottom-Dollar Markets</td><td>Tsawassen</ +td><td>Canada</td> </tr> </tbody></table> <input name="ct" value="Submit your Changes" onclick="return C +onfirm();" type="submit"> <span id="Status"></span></form> </div> HTML ;


Thanks for the Help!!!

Replies are listed 'Best First'.
Re: Select data rows from a grid with Perl
by jasonk (Parson) on Dec 23, 2008 at 19:58 UTC

    The way you normally approach this is by also giving the checkboxes a value (and in that case it helps if you give them all the same name, since they are related)...

    #!/usr/bin/perl -w use CGI; my $cgi = CGI->new; print $cgi->header, $cgi->start_html, $cgi->start_form; if ( my @selected = $cgi->param( 'my_checkbox' ) ) { print "<hr>\n"; print "<p>You selected: ".join( ', ', @selected )."</p>\n"; } print "<ul>\n"; for my $x ( 1 .. 100 ) { print qq{<li><input type="checkbox" name="my_checkbox" value="$_"> + $_</li>\n}; } print "</ul>\n"; print $cgi->submit,$cgi->end_form, $cgi->end_html;

    Also look at [cpan://


    www.jasonkohles.com
    We're not surrounded, we're in a target-rich environment!
      For the checkbox values it's OK, what about the rest of the information of the row that the checkbox(s) belong to, like:
      <td><input id="" name="ctl1" type="checkbox"></td> <td>Alfreds Futterkiste</td> <td>Berlin</td> <td>Germany</td>

      I have checked this checkbox, I need to send the rest of the information with it, thats what I am looking for. After the check in front of this checkbox I would get:
      Alfreds Futterkiste Berlin Germany
        What about passing all the values I am looking for inside the checkbox tag like:
        <td width=\"400\" align=\"center\"> <input name=\"pass_values\" value=\"$value1 $value2 $value3 $value4 $ +value5 $more_values_here\" type=\"checkbox\" /> Check</td>

        Is this a way around this, or is there a better way to do it?