[sf-perl] Bulk Delete check boxes

Michael Friedman friedman at highwire.stanford.edu
Tue Feb 26 22:25:08 PST 2008


Ken,

What I've done to handle lists like this using CGI is to name all the  
checkboxes the same thing, but give them different values. Then when  
you read the param back from CGI, you get an array of the values of  
those boxes. (You still set 'checked' status to determine how they  
will look on the HTML page.)

As for if you return to the same CGI script or a different one, that  
depends a lot on your personal style. In my office, we have some  
people who prefer one large, multi-faceted script and others who  
prefer several smaller single-focus scripts.

I prefer several specific scripts, as I find them easier to maintain,  
but most of my "working" code is in modules anyway. Each of those  
scripts is merely a wrapper that reads the CGI input, converts it to  
parameters and calls module functions that do the "real" work.

In fact, I've used a CGI script that was a generic "confirmation  
page". It took as POST fields the list of strings to display, the name  
of the OK and Cancel buttons, and the URL of the script to send the  
form output to once the user had confirmed their action.  
Unfortunately, it's University property and I can't share it in full,  
but you can probably build a script like that in relatively short  
order if it would fill your needs.

The flow of this system looks like: (monospaced font works best)

         display     confirm     delete
         table       action      rows
           |            |         |
--------->|            |         |
           |            |         |
<---------|            |         |
(pick rows)            |         |
---------------------->|         |
                        |         |
<----------------------|         |
(yes I'm sure)                   |
-------------------------------->|
                                  |
<--------------------------------|

where:
"display table" is a CGI script that reads the DB and displays the  
table, with checkboxes.

"confirm action" is the action attribute on the form returned by  
"display table". It reads the checked boxes and produces a page  
listing only the ones you've checked with an "Are you sure?" button.

"delete rows" is the action attribute on the confirmation form and  
actually does the deleting.

Be sure that all the data you need to delete the rows is present in  
the returned page from the "confirm action" script. Otherwise you'll  
get to "delete rows" and won't know what to delete. For this it's  
often helpful to use hidden form fields.

*BIG GIANT SECURITY WARNING* Passing information around in form fields  
like this is inherently insecure. If you care about someone tapping  
into the middle of the pipe and deleting rows maliciously, you  
probably want to look into another solution. One such solution is a  
session-tracking db. Another is Continuity. For internal applications,  
you usually don't care about someone hacking the app, as they could  
just mangle the db directly anyway. :-)

     http://search.cpan.org/~awwaiid/Continuity-0.97/lib/Continuity.pm


Good luck!
-- Mike


On Feb 26, 2008, at 4:42 PM, ken uhl wrote:

> Hi,
> maybe this will illustrate what I am trying to do:
>
>
>
> shebang perl -
> #
>
> # BULK_delete.pl
>
>
> use CGI;
> my $sq = new CGI;
>
> use DBI;
> my $db = new DBI;
>
> # code snipped from library module
>
>    my $db_query = "select * from table";
>    my $dbh = $db->pepare($db_query);
>    my @db_queryresults = $dbh execute();
>
> # render the DB query to HTML table
>     my @lines ; # array for print
>
>
> for my $db_line (@db_queryresults) {
> # get the table row id from results
>    $hrowid = @db_queryresults->[4];
>
> # make a uniquely named check box
>    my $be_bad =   $sq->checkbox( -name => "bulkdeleteHost_$hrowid",
> -checked => 0, -value => "FALSE", -label => " Bulk  Delete ") ;
>
> #   append check box to html table
> push(@lines, $sq->td($be_bad) );
>
> # append some other data from table to display
>
> push(@lines, $sq->td(@db_queryresults->[2] ));
>
>
> print @lines;
>
> ...
>
> #   from library
>   1708 sub _dnsURL {
>   1709   my $q = shift;        # CGI handle
>   1710   my $dir = shift;      # CGI subdirectory
>   1711   my $script = shift;   # script to run
>   1712   my $qparam = shift;   # hasref to additional named CGI params
>   1713
>   1714   my $sq = new CGI;
>   1715   $sq->delete_all;
>   1716   $sq->param(-name => 'AWSAuthToken', -value =>
> $q->param('AWSAuthToken'));
>   1717   $sq->param(-name => 'AWSSignatureBase64',
>   1718       -value => $q->param('AWSSignatureBase64'));
>   1719
>   1720   foreach (keys %$qparam) {
>   1721       $sq->param(-name => $_, -value => $qparam->{$_});
>   1722   }
>   1723
>   1724   return "/cgi-bin/dns-db/" . "$dir/$script?" .
> $sq->query_string;
>   1725 }
>
>
> # ------------------------------
>
>
> Question :
>
> This displays HTML table of data base rows with Bulk Delete check box.
>
> how do I render a confirmation page of just the selected records?
>
> Should I do this in the same CGI  script, or pass all the data to
> another script?
>
> I looked at CGI::checkbox_goup  as a means to parse all the  
> values,do I
> need another action /submit button to drive that ?
>
> Ken
> berkeley
>
>
> _______________________________________________
> SanFrancisco-pm mailing list
> SanFrancisco-pm at pm.org
> http://mail.pm.org/mailman/listinfo/sanfrancisco-pm

---------------------------------------------------------------------
Michael Friedman                     HighWire Press
Phone: 650-725-1974                  Stanford University
FAX:   270-721-8034                  <friedman at highwire.stanford.edu>
---------------------------------------------------------------------



More information about the SanFrancisco-pm mailing list