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

Hello, I have got multiple checkboxes on a form with dates pulled from a form like this:
... <form method="post" action="$ENV{'SCRIPT_NAME'}?action=add_2&Month=$se +lected_month&Year=$selected_year"> <table align="center" border="0" cellpadding="1" cellspacing="0" width +="100%"> ~; print qq~<tr>\n~; @data = (PTG1, PTG2, PTG3, PTG4, F1, F2, F3, F4, R1, R2, R3, R4, J +S1, JM2, JL3, JX4); foreach $key(@data) { print qq~<td><b>$key</b></td>\n~; } print qq~</tr> <tr> ~; foreach $key(@data) { print qq~<td> <select name="$key" size="13" multiple> <option value="-1" selected></option> ~; $days_in_this_month = $days_in_month[$input{'Month'}]; if ( $input{'Month'} == 2 and leap_year($input{'Year'})) { $d +ays_in_this_month = 29; } for ($i=1; $i<=$days_in_this_month; $i++) { print qq~<option value="$i">$i</option>\n~; } print qq~</select> </td> ~; } print qq~</tr> </table> <center> <input type="submit" name="submit"> <input type="reset"> </center> </form> ...
How can I get the complete input of the form and write it into a file without using cgi.pm? I tried it with something like this, but that doesn't work:
... open (DAT, ">$db_file"); if ($flock eq "y") { flock DAT, 2; } for ($i=1; $i<=$days_in_this_month; $i++) { print qq~$i. $input{'Month'}. $input{'Year'}~; foreach $i (1..16) { foreach $i (keys (%input)) { if ($input{'$i +'} == -1) { $input{'$i'} = "---"; } else { $input{'$i'} = "$name"; } print qq~|$input{'$i'}~; } } print DAT qq~\n~; } ...
As you can see I'm a complete perl-newbie and would be very grateful for any help ;-)

Replies are listed 'Best First'.
Re: Multpile checkbox handling?
by LD2 (Curate) on Apr 14, 2001 at 21:28 UTC
    Why would you not want to use CGI.PM? I think it would actually work well, look at this - document.

    Update: Also, check out Ovid's course.
Re: Multpile checkbox handling?
by kha0z (Scribe) on Apr 14, 2001 at 23:50 UTC
    If I am not mistaken, this is another example of rolling on your own. CGI.pm already has the API built so that it is easy to gather the data that the user provided. I would recommend that you look closer at CGI.pm and see how it can solve this problem for you. That way you don't have to make sure that your API is robust enough to handle changes to the website.

    This is a good introduction to CGI.pm. You may want to really ask yourself why you don't want to use CGI.pm.

    Good Hunting,
    kha0z

Re: Multpile checkbox handling?
by eejack (Hermit) on Apr 15, 2001 at 09:33 UTC
    Personally I would recommend using CGI.pm, but let's say you have a really good reason for not using it there are a few things to look at to maybe help you along the way...

    You are sending information from your form via POST and GET methods. Normally this will not work, so at least a portion of your information is being ignored, which could be your whole problem.

    You don't describe how you are retrieving your data, but I would look into these two variables:

    $ENV{'CONTENT_LENGTH'} #normally used with POST method $ENV{'QUERY_STRING'} #normally used with GET method
    to see where your information is and how it is being delivered.

    But I would use CGI.pm ( you might also want to give -w and use strict a spin as well).

    EEjack

      Actually $ENV{'CONTENT_LENGTH'} is just the length of the POST'ed data. The actual data read from STDIN...

      Greetz
      Beatnik
      ... Quidquid perl dictum sit, altum viditur.
Re: Multpile checkbox handling?
by Anonymous Monk on Apr 15, 2001 at 23:14 UTC
    Hi, it's me again ;-) I'm using the following code to get the input:
    $ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/; if ($ENV{'REQUEST_METHOD'} eq "POST"){ read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); } else { $buffer = $ENV{'QUERY_STRING'}; } @pairs = split(/&/, $buffer); foreach $pair (@pairs){ ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%(..)/pack("C", hex($1))/eg; $input{$name} = $value; }
    The Problem is, my script above writes simply nothing into the database. Anybody has an idea, what I'm doing wrong? Thanks again GG
      You are not printing this data to your database, you are just printing, except in the one line where you print to DAT....

      Also, if I am reading it correctly, you are going through all of your values in the hash input 16 times. It looks like your routine will assign a value of either three dashes or the last value of $name from the part of your script that deals with inputting. This will probably clobber all of your work of assigning these values.

      You might want to try this in your script to see what the values are...

      foreach $key (keys (%input)) { print qq|The key is $key and the value is $input{$key}\n|; }
      To see what effect your loop has on the data, try running it above and below where you trying to write to the database. I often do this to doublecheck I am not messing up my values (which I do with amazing frequency).

      I still recommend CGI.pm BTW - makes lots of things a bit easier.

      HTH,

      Kind thanks to Beatnik on his gentle clarification of the real value of $ENV{'CONTENT_LENGTH'}
      EEjack