in reply to Loading $_ as checkbox value when $_ has double quotes in its value

OK, Now I got a beautiful form with appropriate checkbox values. To those who have re-asserted my need to understand CGI.pm I agree and the Mouse is in the mail to me now, the Rat was less than helpful.
Problem is that I can't seem to figure why I end up with an empty array no matter whether boxes are checked or not. I am assuming the array is empty cause I always end up with an empty txt file after I submit the form. 0 bytes in size it is. Have I made yet another dopey neophyte error?
Thanks again, and again, and.....
jg
#!/usr/bin/perl -w use strict; use CGI; use CGI ':standard'; use CGI::Carp qw/fatalsToBrowser /; my $q = CGI->new(); my $url="http://www.xxxxx.com/xxx/xxx/"; my $path_to_text="/home/xxxxx/www/xxx/xxx/body.txt"; my $this_script_url="http://www.xxxxxx.com/cgi-bin/xxxxx.cgi"; my $post_number; my @updated_text; $/="\n\n\n"; #set the input Record seperator to a tri +ple newline if ( $q->param() ) { #if params are not undef # ignore deleted next if ( $q->param("box$post_number") ); # keep the rest s/&quot;/"/g; s/&apos;/'/g; push @updated_text, $_; # now update file open(FH,">$path_to_text") || die $!; print FH join "\n\n\n", @updated_text; close(FH); print "Location: $url\n\n"; } else { #if params are undef open (FH, "$path_to_text") or die "where's the damn file? : $!"; print "Content-type: text/html\n\n"; print qq|<HTML><BODY><center><form method="post" action="$this_scr +ipt_url">\n <TABLE width=600><TR bgcolor=#000066><TD colspan=2 align=c +enter> <font color=#ffffff><h2>Edit Your Posts</h2></font></TD><T +R><TD>|; while (<FH>) { next if /^\n+$/; #ignore lines which contains + only newlines $post_number++; chomp; s/"/&quot;/g; s/'/&apos;/g; print qq|Delete This Post? \n <input type=checkbox name="box$post_number" value=" $_ " c +hecked><p> $_<hr>\n<p>\n<br><br><br>\n|; } close FH; print "</TD></TR><TR bgcolor=#333333><TD align=center colspan=2> +\n"; print "<input type=submit name=Submit value=\"Delete Checked Pos +ts\"></TD>"; print "</form></center></table></body></html>"; } exit;
_____________________________________________________
If it gets a little bit out of hand sometimes, don't let it fool you into thinkin' you don't care.TvZ
  • Comment on Re: Loading $_ as checkbox value when $_ has double quotes in its value
  • Download Code

Replies are listed 'Best First'.
Re: Re: Loading $_ as checkbox value when $_ has double quotes in its value
by virtualsue (Vicar) on Dec 05, 2001 at 21:53 UTC
    From your program:
    if ( $q->param() ) { #if params are not undef # ignore deleted next if ( $q->param("box$post_number") );
    Here your code checks to see if there are any params, which is good, but the next line is an out-of-place next. You don't have any logic in place to loop through the individual params themselves. $q->params() contains only the names of all the checkboxes which were selected (checked) along with your submit button value and anything else in the form. When you are at this point in the script (after submit), you don't know which checkboxes are going to be in $q->params(), if any.

    Therefore you need to examine $q->params(), look at the checkbox values and associate them with the appropriate "post" text in your file. Here's a little bit of code that I hope will get you started:

    # Pull out the numbers of the checkboxes that were selected. # e.g. If box1, box3 and box12 were selected, @selected = (1, 3, 12) my @selected = grep { $_=$1 if /^box(\d+)/; } $q->params(); open FH, $filename or die "Can't read $filename, $!"; # open for read my @posts = <FH>; # This should delete the post text associated with each selected check +box # You have to use the @selected array in reverse. I leave the # explanation for this as an exercise for you. :) splice @posts, ($_ - 1), 1 for reverse @selected; # Now write the file back out again open FH, ">$filename" or die "Can't write to $filename, $!"; print FH @posts or die "Couldn't write to file, $!"; close FH or die "Can't close file etc, $!";

    BTW I think you need to use &#039; rather than ' when encoding single quotes. ' is an XML character entity.

Re: Re: Loading $_ as checkbox value when $_ has double quotes in its value
by atcroft (Abbot) on Dec 06, 2001 at 07:48 UTC

    Having just written code for handling selections of things for a recent job, I was curious about your code when I saw the posting. I took a different tact, though, but a few thoughts that might help.

    First of all, my understanding of CGI.pm's handling of checkboxes is that if in your form you have a set of checkboxes named "boxes" such as <input type=checkbox name=boxes value="whatever" checked> , $q->param("boxes") returns a list of the values for the input boxes checked (including "whatever" for this one, unless unchecked). So you might do something like @todelete = $q->param("boxes"); then loop thru them.

    Also, you might consider testing if $q->param("submit") is defined, then go from there, rather than seeing if any parameters are defined.

    (I also posted a link to this response in your followup article, Shouldn't this return the named checkbox's value? )